Skip to content

Instantly share code, notes, and snippets.

@SetiZ
Created December 30, 2019 15:33
Show Gist options
  • Save SetiZ/e96ad3a816a686c1fc9ea6c64906a176 to your computer and use it in GitHub Desktop.
Save SetiZ/e96ad3a816a686c1fc9ea6c64906a176 to your computer and use it in GitHub Desktop.
import cv2
import imutils
import time
import numpy as np
class VideoCamera(object):
def __init__(self, flip = False):
self.vs = cv2.VideoCapture(0)
self.flip = flip
time.sleep(2.0)
def __del__(self):
self.vs.stop()
def flip_if_needed(self, frame):
if self.flip:
return np.flip(frame, 0)
return frame
def get_frame(self):
ret, frame = self.vs.read()
frame = cv2.flip(frame, 1) # use it if you want to flip the image or not
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
def get_object(self, classifier):
found_objects = False
ret, frame = self.vs.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
objects = classifier.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
if len(objects) > 0:
found_objects = True
# Draw a rectangle around the objects
for (x, y, w, h) in objects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
ret, jpeg = cv2.imencode('.jpg', frame)
return (jpeg.tobytes(), found_objects)
@MohamadAlsadi
Copy link

I am using USB CAM
I try your code but not work
when I run main.py I get the message :

Error sending email: <class 'cv2.error'>

@SetiZ
Copy link
Author

SetiZ commented Jul 20, 2020

unfortunately this codec doesn't work with USB cam. You will need to use a pi cam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment