So I created a class to open VideoCapture() and read frames using opencv.
import cv2
import imutils
class Camera():
def __init__(self):
self.cap = cv2.VideoCapture(0) # Prepare the camera...
print("Camera warming up ...")
self.ret, self.frame = self.cap.read()
def get_frame(self):
self.frames = open("stream.jpg", 'wb+')
s, img = self.cap.read()
if s: # frame captures without errors...
cv2.imwrite("stream.jpg", img) # Save image...
return self.frames.read()
def main():
while True:
cam1 = Camera().get_frame()
frame = imutils.resize(cam1, width=640)
cv2.imshow("Frame", frame)
return ()
if __name__ == '__main__':
main()
This gives me error as:
(h, w) = image.shape[:2]
AttributeError: 'bytes' object has no attribute 'shape'
Also, when I remove the get_frame function and directly created a constructor like this:
cam1 = Camera()
frame = imutils.resize(cam1.frame, width=640)
The camera object is created recursively created. Can someone help about what I am doing wrong here.