简体   繁体   中英

Error when running object detection model with webcam PyTorch

I'm trying to run my custom model with my webcam as the source for live object detection, however I get the error:

cv2.error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'imshow'
> Overload resolution failed:
>  - imshow() missing required argument 'mat' (pos 2)
>  - imshow() missing required argument 'mat' (pos 2)
>  - imshow() missing required argument 'mat' (pos 2)

My code is as follows:

import torch
import cv2

# Model
model = torch.hub.load('ultralytics/yolov5', 'custom', 'best.pt')


# Image
vid = cv2.VideoCapture(0, cv2.CAP_DSHOW)
vid.open(0)


while (True):

    # Capture the video frame
    # by frame
    ret, frame = vid.read()

    # Display the resulting frame
    results = model(frame)

    results.render()
    cv2.imshow(results)

    # the 'q' button is set as the
    # quitting button you may use any
    # desired button of your choice
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()

What am I doing wrong here? I think results isn't in the correct format that cv2.imshow wants but I do not quite know what I need to do to rectify this, thx!

cv2.imshow needs a window title. cv2.imshow("some window title", results)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM