I am trying to make a object detection program which detects the object infront of the user and outputs the object using tts. What i'm trying to do is display the processed frame in a tkinter label. I have tried using PIL but i always get src_empty error when updating the frames.
def run_computer_vision():
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
classes = []
with open("yolov3.txt", "r") as f:
classes = [line.strip() for line in f.readlines()]
# Reading layers
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
cap = cv2.VideoCapture(0)
# loading the image
while True:
ret, frame = cap.read()
if not ret:
break
height, width, channels = frame.shape
blob = cv2.dnn.blobFromImage(
frame, 0.00392, (416, 416), (0, 0, 0), True, False)
net.setInput(blob)
outs = net.forward(output_layers)
boxes = []
confidences = []
class_ids = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0:
cx = int(detection[0] * width)
cy = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(cx - w / 2)
y = int(cy - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# apply non-max suppression
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# loop over the boxes
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = classes[class_ids[i]]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, label, (x, y + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
direction = get_direction(x, y, w, h, width, height)
engine.say(label + "On your " + direction)
engine.runAndWait()
time.sleep(1)
cv2.imshow("Detection", frame)
# Check for key presses
key = cv2.waitKey(1)
if key == ord('q'):
break
elif key == ord('s'):
cv2.imwrite("image.jpg", frame)
# Release the camera and close the window
cap.release()
cv2.destroyAllWindows()
Camera()
I would like to display the frames of cv2.imshow("Detection",frame)
in a tkinter label