Ask Your Question
0

Opencv can't show live webcam footage.What is wrong with my code ?

asked 2020-07-02 15:26:01 -0600

I want to display contour with live webcam feeding but it only show a frame.

code:

import cv2
import numpy
import math


windowName = "OBJECT_MOVEMENT"
cv2.namedWindow(windowName)

cap = cv2.VideoCapture(0)

if cap.isOpened():
ret, frame = cap.read()
else:
    ret = False
    print("False operation.")
while ret:
    ret, frame=cap.read()
    imgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    finalIm = cv2.drawContours(frame, contours, -1, (0,255,0), 1)

    cnt = contours[0]
    M = cv2.moments(cnt)
    contours_sizes = [(cv2.contourArea(cnt), cnt) for cnt in contours]
    biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]

    cX = int(((M["m10"])+1) / ((M["m00"])+1))
    cY = int(((M["m10"])+1) / ((M["m00"])+1))

    cv2.imshow("Countours",frame)
    if cv2.waitKey(0) == ord('q'):
        break
    cap.release()
edit retag flag offensive close merge delete

Comments

broken indentation here, can you fix it, please ?

berak gravatar imageberak ( 2020-07-02 15:55:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-07-03 06:54:38 -0600

supra56 gravatar image

updated 2020-07-03 08:05:08 -0600

There is wrong on my side that I used OpenCV4.3.0. The problem is waitKey(0). Change index to 1. Actually, I'm uncertainly on your side.. But it should be worked. I change boolean for python 3.8.

#!/usr/bin/env python35
#OpenCV 4.3.0, Raspberry pi3B/+, 4b/4g/8g, Thonny 3.7.3
#Date: 1st July, 2020

import cv2
import numpy
import math

windowName = "OBJECT_MOVEMENT"
cv2.namedWindow(windowName)
cap = cv2.VideoCapture(0)

if cap.isOpened():
    ret, frame = cap.read()
else:
    ret = False
    print("False operation.")

while ret:
    ret, frame = cap.read()
    if ret:
        imgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        ret, thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY)
        contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        finalIm = cv2.drawContours(frame, contours, -1, (0,255,0), 1)

        cnt = contours[0]
        M = cv2.moments(cnt)
        contours_sizes = [(cv2.contourArea(cnt), cnt) for cnt in contours]
        biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]

        cX = int(((M["m10"])+1) / ((M["m00"])+1))
        cY = int(((M["m10"])+1) / ((M["m00"])+1))

        cv2.imshow("OBJECT_MOVEMENT",frame)

        if cv2.waitKey(1) & 0xFF is ord('q'):
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()

Instead of this:

if cap.isOpened():
    ret, frame = cap.read()
else:
    ret = False
    print("False operation.")

while ret:
    ret, frame = cap.read()
    if ret:

Must better:

# Capture several frames to allow the camera's autoexposure to adjust.
for i in range(10):
    ret, frame = cap.read()
if not ret:
    exit(1)

while ret:
    ret, frame = cap.read()
    if ret
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-07-02 15:26:01 -0600

Seen: 347 times

Last updated: Jul 03 '20