Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SystemError: <built-in function putText> returned NULL without setting an error

Tags:

python

cv2

when I trying to detect the objects of products and those names with the below code. Here I am using the cv2.putText() function, but getting the below error. Could anyone please help me.

from cProfile import label
from tkinter import font

import cv2
import numpy as np

net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
classes = []
with open("coco.names", "r") as f:
    classes = [line.strip() for line in f.readlines()]
#print(classes)
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] -1] for i in net.getUnconnectedOutLayers()]


img = cv2.imread("amz.jpg")
img = cv2.resize(img, None, fx=0.9, fy=0.9)
height, width, channels = img.shape

blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0,), True, crop=False)

for b in blob:
    for n, img_blob in enumerate(b):
        cv2.imshow(str(n), img_blob)

net.setInput(blob)
outp = net.forward(output_layers)
print(outp)

class_ids = []
confidences = []
boxes = []

for out in outp:
    for detection in out:
        scores = detection[5:]
        class_id = np.argmax(scores)
        confidence = scores[class_id]
        if confidence > 0.5:

            center_x = int(detection[0] * width)
            center_y = int(detection[1] * height)
            w = int(detection[2] * width)
            h = int(detection[3] * height)
            x = int(center_x - w / 2)
            y = int(center_y - h / 2)
            boxes.append([x, y, w, h])
            confidences.append(float(confidence))
            class_ids.append(class_id)
#print(len(boxes))
number_object_detected = len(boxes)
#font = cv2.FONT_HERSHEY_PLAIN
font = cv2.FONT_HERSHEY_SIMPLEX
for i in range(len(boxes)):
    x, y, w, h = boxes[i]
    lable = classes[class_ids[i]]
    print(lable)
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.putText(img, label, (x, y + 30), font, 1, (0, 0, 0), 3, cv2.LINE_AA, True
 cv2.imshow("Image", img)
 #img = cv2.resize(img, None, fx=0.9, fy=0.9)
 cv2.waitKey(10000)`enter code here`

error:

Traceback (most recent call last): File "C:/Users/Gajapati/PycharmProjects/yolo/yolo-opencv.py", line 59, in cv2.putText(img, label, (x, y + 30), font, 1, (0, 0, 0), 3, cv2.LINE_AA, True) SystemError: returned NULL without setting an error

like image 442
kesava Avatar asked Jun 05 '20 11:06

kesava


Video Answer


1 Answers

The text should be string:

cv2.putText(img, str(label), (x, y + 30), font, 1, (0, 0, 0), 3, cv2.LINE_AA, True)
like image 70
OH SANGJIN Avatar answered Sep 25 '22 00:09

OH SANGJIN