resize.cpp:4052: 错误: (-215:断言失败) !ssize.empty() 在函数 'cv::resize'
我正在从我的应用程序中拍摄照片并将其上传到烧瓶上,在烧瓶上我正在运行下面的Python代码以从烧瓶中获取最近上传的图像并执行检测并从图像中提取车辆的车牌号。 我遇到的实际错误是图像正在安全且良好地上传到烧瓶上,但以上述错误的形式显示对检测无效。
from fileinput import filename
from flask import Flask, request, jsonify
import cv2
import imutils
import numpy as np
import pytesseract
import werkzeug
app = Flask(__name__) #creating flask server
@app.route('/upload', methods=["POST"]) #defining the route of pages
def upload():
if(request.method == "POST"):
imagefile = request.files['image']
imagefile2=str(imagefile)
# print('12132')
filename = werkzeug.utils.secure_filename(imagefile.filename)
imagefile.save("./uploadedimages/"+filename)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
img = cv2.imread(imagefile,cv2.IMREAD_COLOR)
# print (imagefile2)
img = cv2.resize(img, dsize=(600,400) )
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 13, 15, 15)
edged = cv2.Canny(gray, 30, 200)
contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
if screenCnt is None:
detected = 0
print ("No contour detected")
else:
detected = 1
if detected == 1:
cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)
mask = np.zeros(gray.shape,np.uint8)
new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)
new_image = cv2.bitwise_and(img,img,mask=mask)
(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx+1, topy:bottomy+1]
text = pytesseract.image_to_string(Cropped, config='--psm 11')
print("programming_fever's License Plate Recognition\n")
print("Detected license plate Number is:",text)
img = cv2.resize(img,(500,300))
Cropped = cv2.resize(Cropped,(400,200))
cv2.imshow('car',img)
cv2.imshow('Cropped',Cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
return jsonify({
"message":"Uploaded"
})
if __name__ == "__main__":
app.run(debug=True, port=4000 )
我尝试过将图像转换为字符串,但没有成功。我正在使用 flutter 作为应用程序。 下面是接收烧瓶响应的应用程序屏幕代码。
uploadImage() async {
final request = http.MultipartRequest(
"POST", Uri.parse("https://6a6d-59-103-181-239.ngrok.io/upload"));
final headers = {"Content-type": "multipart/form-data"};
request.files.add(http.MultipartFile('image',
selectedImage.readAsBytes().asStream(), selectedImage.lengthSync(),
filename: selectedImage.path.split("/").last));
request.headers.addAll(headers);
final response = await request.send();
http.Response res = await http.Response.fromStream(response);
final resJson = jsonDecode(res.body);
message = resJson['message'];
setState(() {});
}
I am taking the picture from my app and uploading it on flask and on flask I am running the below python code to take the recently uploaded image from flask and perform the detection and extracting the number plate of vehicle from the image.
The actual error I ran into is the image is uploading on flask safe and fine but is showing invalid for the detection in the form of above error.
from fileinput import filename
from flask import Flask, request, jsonify
import cv2
import imutils
import numpy as np
import pytesseract
import werkzeug
app = Flask(__name__) #creating flask server
@app.route('/upload', methods=["POST"]) #defining the route of pages
def upload():
if(request.method == "POST"):
imagefile = request.files['image']
imagefile2=str(imagefile)
# print('12132')
filename = werkzeug.utils.secure_filename(imagefile.filename)
imagefile.save("./uploadedimages/"+filename)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
img = cv2.imread(imagefile,cv2.IMREAD_COLOR)
# print (imagefile2)
img = cv2.resize(img, dsize=(600,400) )
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 13, 15, 15)
edged = cv2.Canny(gray, 30, 200)
contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
if screenCnt is None:
detected = 0
print ("No contour detected")
else:
detected = 1
if detected == 1:
cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)
mask = np.zeros(gray.shape,np.uint8)
new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)
new_image = cv2.bitwise_and(img,img,mask=mask)
(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx+1, topy:bottomy+1]
text = pytesseract.image_to_string(Cropped, config='--psm 11')
print("programming_fever's License Plate Recognition\n")
print("Detected license plate Number is:",text)
img = cv2.resize(img,(500,300))
Cropped = cv2.resize(Cropped,(400,200))
cv2.imshow('car',img)
cv2.imshow('Cropped',Cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
return jsonify({
"message":"Uploaded"
})
if __name__ == "__main__":
app.run(debug=True, port=4000 )
I have tried converting the image into string but didn't worked out. I am using flutter for app.
And below the is the app screen code where the response of the flask is being recieved.
uploadImage() async {
final request = http.MultipartRequest(
"POST", Uri.parse("https://6a6d-59-103-181-239.ngrok.io/upload"));
final headers = {"Content-type": "multipart/form-data"};
request.files.add(http.MultipartFile('image',
selectedImage.readAsBytes().asStream(), selectedImage.lengthSync(),
filename: selectedImage.path.split("/").last));
request.headers.addAll(headers);
final response = await request.send();
http.Response res = await http.Response.fromStream(response);
final resJson = jsonDecode(res.body);
message = resJson['message'];
setState(() {});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenCV 的
cv2
绑定以神秘的方式告诉您您无意中传递了None
。这里可能返回 None,可能是因为您打算传递
而不是
imagefile
。cv2
bindings for OpenCV have cryptic ways of telling you that you inadvertently passedNone
. Hereis likely returning None, possibly because you meant to pass
instead of
imagefile
.