'resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
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(() {});
}
Solution 1:[1]
cv2 bindings for OpenCV have cryptic ways of telling you that you inadvertently passed None. Here
img = cv2.imread(imagefile,cv2.IMREAD_COLOR)
is likely returning None, possibly because you meant to pass
"./uploadedimages/"+filename
instead of imagefile.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dave W. Smith |
