Little OpenCV bug

Hello there, guys.

Back with another issue. This time to do with OpenCV. So I have an object detection project written with the guidance of a course which can detect objects and indicate what they are (and how accurate they are). However, instead of the program constantly displaying video(as it did in the course), it just sends a pop-up of whatever image it finds and in order for the program to continue you have to close the screenshot before it gives you a new one. Essentially, it just screenshots the thing and gives the user the screenshot instead of showing you in real-time. I have never used OpenCV before and therefore have no idea how to do this.

Here is the code I have written so far:

import cv2

thres = 0.5

# img = cv2.imread('lena.png')
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)

classNames = []
classFile = 'coco.names'
with open(classFile, 'rt') as f:
    classNames ='\n').split('\n')

config_path = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weights_path = 'frozen_inference_graph.pb'

net = cv2.dnn_DetectionModel(weights_path, config_path)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))

while True:
    success, img =
    class_ids, confs, bbox = net.detect(img, confThreshold=thres)
    print(class_ids, bbox)
    if len(class_ids) != 0:
        for class_id, confidence, box in zip(class_ids.flatten(), confs.flatten(), bbox):
            cv2.rectangle(img, box, color=(0, 0, 255), thickness=3)
            cv2.putText(img, classNames[class_id - 1].upper(), (box[0] + 10, box[1] + 30),
                        cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2)
            cv2.putText(img, str(round(confidence * 100, 2)), (box[0] + 200, box[1] + 30),
                        cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
    cv2.imshow("Output", img)

If anyone could help me on this, it would be greatly appreciated!

This topic was posted a while back, but the situation is very timely and simple to answer. Rather than have a dead-end if Google includes this topic in search results, the answer is below.

NOTE: This topic is also worth a fresh look because it illustrates the liability of trying to learn a complex application of a system before FULLY drilling and exercising your way through the basics and fundamentals. Otherwise the code will contain dozens of minor gremlins like this that will all create bugs and errors and these baby gremlins will prevent the code from running properly just like their big brother ands sister major bugs and errors. The topic title is accurate; this is a very little bug.

…needs to have a nonzero value like waitkey(1) to produce live streaming updates. With a zero, waitkey(0) stops and just waits for a keypress. With waitkey(1000), it will wait 1000 milliseconds (one second) and continue.

At first look this is a counterintuitive behavior: Since waitkey(n) is supposed to “wait ‘n’ microseconds and then continue execution”, one would suspect that a value of 0 means “wait 0 microseconds”. HOWEVER, as is often the case, the value 0 is a switch that does something special. In this case it means “wait infinite milliseconds” (in other words, “wait forever”).

This understanding of waitkey() in OpenCV is the bookend to print("hello world!"). The three most fundamental functions in OpenCV are:

imread()  loads an image from a file
imshow()  queues an image for display
waitkey()  triggers the image display(s)