Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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()

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