我在文档扫描仪上有一个opencv脚本,我想创建一个post请求,将扫描的图像作为响应返回
当我试图通过post请求读取图像(使用opencv)时,我得到以下错误
TypeError at /documentScanner/
Can't convert object of type 'FieldFile' to 'str' for 'filename'
Request Method: POST
Request URL: http://127.0.0.1:8000/documentScanner/
Django Version: 3.1.5
Exception Type: TypeError
Exception Value:
Can't convert object of type 'FieldFile' to 'str' for 'filename'
Exception Location: C:\Users\8470p\Downloads\flask-document-scanner-master\Scanner\documentScanner\scan.py, line 30, in Scanner
Python Executable: C:\Users\8470p\Anaconda3\envs\DocumentScanner\python.exe
Python Version: 3.7.9
Python Path:
['C:\\Users\\8470p\\Downloads\\flask-document-scanner-master\\Scanner',
'C:\\Users\\8470p\\models-master',
'C:\\Users\\8470p\\models-master\\research',
'C:\\Users\\8470p\\models-master\\research\\slim',
'C:\\Users\\8470p\\models-master\\research\\object_detection',
'C:\\Users\\8470p',
'C:\\Users\\8470p\\Desktop\\tf_1.12\\models\\research\\object_detection',
'C:\\Users\\8470p\\Desktop\\tf_1.12\\models\\research\\slim',
'C:\\Users\\8470p\\Desktop\\tf_1.12\\models\\research',
'C:\\Users\\8470p\\Desktop\\weed1\\models\\research\\object_detection',
'C:\\Users\\8470p\\Desktop\\weed1\\models\\research\\slim',
'C:\\Users\\8470p\\Desktop\\weed1\\models\\research',
'C:\\Users\\8470p\\AppData\\Local\\Programs\\Python\\Python37\\Scripts',
'C:\\Users\\8470p\\Downloads\\flask-document-scanner-master\\Scanner',
'C:\\Users\\8470p\\Anaconda3\\envs\\DocumentScanner\\python37.zip',
'C:\\Users\\8470p\\Anaconda3\\envs\\DocumentScanner\\DLLs',
'C:\\Users\\8470p\\Anaconda3\\envs\\DocumentScanner\\lib',
'C:\\Users\\8470p\\Anaconda3\\envs\\DocumentScanner',
'C:\\Users\\8470p\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Users\\8470p\\Anaconda3\\envs\\DocumentScanner\\lib\\site-packages']
Server time: Wed, 06 Jan 2021 22:44:56 +0000
我有什么办法可以解决这个问题。我尝试过各种方法,但不适合我
Modey.py
class ImageScanner(models.Model):
name= models.CharField(max_length=500)
date = models.DateTimeField(auto_now_add=True)
ImageFile = models.FileField(upload_to=upload_to)
def __str__(self):
return self.name + ": " + str(self.ImageFile)
def save(self, *args, **kwargs):
super(ImageScanner, self).save(*args, **kwargs)
Img = Scanner(self.ImageFile)
Opencv脚本
def Scanner(image):
now = timezone.now()
# load the image and compute the ratio of the old height
# to the new height, clone it, and resize it
# image = ImageScanner.objects
# imagefile = image.ImageFile
# image = np.asarray(bytearray(imagefile.read()), dtype="uint8")
# image = cv2.imdecode(image, cv2.IMREAD_COLOR)
image = cv2.imread(image)
ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)
# convert the image to grayscale, blur it, and find edges
# in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 75, 200)
# show the original image and the edge detected image
print ("STEP 1: Edge Detection")
cv2.imshow("Image", image)
cv2.imshow("Edged", edged)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# find the contours in the edged image, keeping only the
# largest ones, and initialize the screen contour
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
# loop over the contours
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# if our approximated contour has four points, then we
# can assume that we have found our screen
if len(approx) == 4:
screenCnt = approx
break
# show the contour (outline) of the piece of paper
print("STEP 2: Find contours of paper")
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
# cv2.imshow("Outline", image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# apply the four point transform to obtain a top-down
# view of the original image
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)
# convert the warped image to grayscale, then threshold it
# to give it that 'black and white' paper effect
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
T = threshold_local(warped, 11, offset = 10, method = "gaussian")
warped = (warped > T).astype("uint8") * 255
# show the original and scanned images
print("STEP 3: Apply perspective transform")
# cv2.imshow("Original", imutils.resize(orig, height = 650))
# cv2.imshow("Scanned", imutils.resize(warped, height = 650))
# cv2.waitKey(0)
cv2.imwrite(os.path.join(f'media/SmatCrow/{now:%Y-%m-%d}/', 'scan_image' +'.jpg'),warped)
seriliazer.py
from rest_framework import serializers
from .models import ImageScanner
from django.contrib.auth.models import User
class ImageScannerSerializer(serializers.HyperlinkedModelSerializer):
date = serializers.DateTimeField(read_only=True)
name = serializers.CharField(read_only=True)
ImageFile = serializers.FileField(read_only=True)
class Meta:
model = ImageScanner
fields = ['id', 'url', 'name', 'date', 'ImageFile']
def create(self, validated_data):
return ImageScanner.objects.create(**validated_data)
view.py
from .models import ImageScanner
from .serializers import ImageScannerSerializer
from rest_framework import viewsets
class ImageScannerView(viewsets.ModelViewSet):
queryset = ImageScanner.objects.all()
serializer_class = ImageScannerSerializer
我可以通过使用python中的
PIL
库来解决这个问题下面是关于如何使用opencv预处理从django数据库读取图像的代码片段
相关问题 更多 >
编程相关推荐