I am on a project that detects the qrcode and its color. I encountered a problem that it randomly popped up an error message. I cannot figure why it would happen, I searched on the internet and I cannot find the answer. In fact, there are no topics about this error!
When there is no QR-code in the webcam, contourArea(src_points)
gives the output 0.0
normally I believe. Somehow it will output 0
and I still cannot figure out why.
However , I stuck at a point where I cannot edit cv2.pyd
to change from contourArea(src_points) > 0.0
to contourArea(src_points) > 0.0 or contourArea(src_points) > 0
.
Code below:
cap = cv2.VideoCapture(0)
lower_red = np.array([3, 60, 60])
upper_red = np.array([5, 255, 255])
while True:
_, img = cap.read()
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
height, width, _ = img.shape
cx = int(width / 2)
cy = int(height / 2)
detector = cv2.QRCodeDetector()
data, bbox, _ = detector.detectAndDecode(img)
if(data and bbox is not None):
print(data)
print(np.int32(bbox))
index_point = np.int32(bbox)
rect = four_point_transform(img, index_point.reshape(4, 2))
mask = cv2.inRange(img_hsv, lower_red, upper_red)
mask_contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(mask_contours) != 0:
for mask_contour in mask_contours:
if cv2.contourArea(mask_contour) > 200:
cv2.putText(img, "STOP", (cx + 200, cy + 230), 0, 1, (255, 255, 255), 4)
x, y, w, h = cv2.boundingRect(mask_contour)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3) # Drawing rectangle
Error message:
Traceback (most recent call last):
File "c:\Users\CSKPHC\Desktop\testing.py", line 58, in <module>
data, bbox, _ = detector.detectAndDecode(img)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\qrcode.cpp:2489: error: (-2:Unspecified error) in function 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl cv::QRCodeDetector::decode(const class cv::_InputArray &,const class cv::_InputArray &,const class cv::_OutputArray &)'
> Invalid QR code source points (expected: 'contourArea(src_points) > 0.0'), where
> 'contourArea(src_points)' is 0
> must be greater than
> '0.0' is 0