def record(): #shutil.rmtree('/home/pi/images/') #os.mkdir('/home/pi/images') #folder = '/home/pi/images/' camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 30 rawCapture = PiRGBArray(camera, size=(640, 480)) timeStamp = str(time.time()) fourcc = cv2.VideoWriter_fourcc('H', '2', '6', '4') video = cv2.VideoWriter(timeStamp + '.h264', fourcc, 20, (640, 480)) clock = 0 for frame in camera.capture(rawCapture, format="bgr", use_video_port=True): image = frame.array startTime = time.clock() image = dc.process(image) cv2.imshow('Video', image) #print(clock) clock += time.clock() - startTime video.write(image) rawCapture.truncate(0) if clock > 30: break video.release() rawCapture.release() cv2.destroyAllWindows()
class Camera: ''' Camera module to abstract between the Raspberry Pi camera or a standard USB camera. Useful for testing and debugging ''' def __init__(self, rpiCam=False, cameraNum=0, width=640, height=480, fps=30): self.rpiCam = rpiCam self.width = width self.height = height self.fps = fps self.cameraNum = cameraNum if self.rpiCam: self.setupRpiCam() else: self.setupUsbCam() def setupRpiCam(self): # import the necessary packages from picamera.array import PiRGBArray from picamera import PiCamera # initialize the camera and grab a reference to the raw camera capture camera = PiCamera() camera.resolution = (self.width, self.height) camera.framerate = self.fps camera.crop = (0.0, 0.0, 1.0, 1.0) self.camera = camera self.rawCapture = PiRGBArray(camera, size=(self.width, self.height)) self.rgb = bytearray(self.width * self.height * 3) self.yuv = bytearray(self.width * self.height * 3 / 2) # wait for camera to warm up time.sleep(0.1) def setupUsbCam(self): # initialize the camera and grab a reference to the raw camera capture self.rawCapture = cv2.VideoCapture(self.cameraNum) # wait for camera to warm up time.sleep(0.1) def grabFrame(self): ''' Grab a single frame from the camera ''' if self.rpiCam: #import yuv2rgb #stream = io.BytesIO() #self.camera.capture(stream, use_video_port=True, format='raw') #stream.seek(0) #stream.readinto(self.yuv) #yuv2rgb.convert(self.yuv, self.rgb, self.width, self.height) #return pygame.image.frombuffer(self.rgb[0:(self.width*self.height*3)], (self.width, self.height), 'RGB') self.camera.capture(self.rawCapture, use_video_port=True, format='bgr') image = self.rawCapture.array self.rawCapture.truncate(0) return image else: _, image = self.rawCapture.read() return image def streamFrames(self): ''' Generator to stream images from the camera as numpy arrays. The pixels are stored as BGR (blue, green, red) ''' if self.rpiCam: # capture frames from the camera for frame in self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True): try: yield frame.array self.rawCapture.truncate(0) except: break finally: self.cleanup() else: while True: try: yield self.grabFrame() except: break finally: self.cleanup() def cleanup(self): if self.rpiCam: self.camera.close() else: self.rawCapture.release() def startPreview(self): if self.rpiCam: self.camera.start_preview() def stopPreview(self): if self.rpiCam: self.camera.stop_preview()
camera.rotation = 180 camera.resolution = (640, 480) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(640, 480)) # allow the camera to warmup time.sleep(0.1) # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # grab the raw NumPy array representing the image, then initialize the timestamp # and occupied/unoccupied text image = frame.array image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.Canny(image, 50, 150, apertureSize=3) # show the frame cv2.imshow("Frame", image) key = cv2.waitKey(1) & 0xFF # clear the stream in preparation for the next frame rawCapture.truncate(0) # if the `q` key was pressed, break from the loop if key == ord("q"): break rawCapture.release() cv2.destroyAllWindows()
class Camera: ''' Camera module to abstract between the Raspberry Pi camera or a standard USB camera. Useful for testing and debugging ''' def __init__(self, rpiCam=False, cameraNum=0, width=640, height=480, fps=30): self.rpiCam = rpiCam self.width = width self.height = height self.fps = fps self.cameraNum = cameraNum if self.rpiCam: self.setupRpiCam() else: self.setupUsbCam() def setupRpiCam(self): # import the necessary packages from picamera.array import PiRGBArray from picamera import PiCamera # initialize the camera and grab a reference to the raw camera capture camera = PiCamera() camera.resolution = (self.width, self.height) camera.framerate = self.fps self.camera = camera self.rawCapture = PiRGBArray(camera, size=(self.width, self.height)) # wait for camera to warm up time.sleep(0.1) def setupUsbCam(self): # initialize the camera and grab a reference to the raw camera capture self.rawCapture = cv2.VideoCapture(self.cameraNum) # wait for camera to warm up time.sleep(0.1) def grabFrame(self): ''' Grab a single frame from the camera ''' if self.rpiCam: self.camera.capture(self.rawCapture, format="bgr") image = self.rawCapture.array self.rawCapture.truncate(0) return image else: _, image = self.rawCapture.read() return image def streamFrames(self): ''' Generator to stream images from the camera as numpy arrays. The pixels are stored as BGR (blue, green, red) ''' if self.rpiCam: # capture frames from the camera for frame in self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True): try: yield frame.array self.rawCapture.truncate(0) except: break finally: self.cleanup() else: while True: try: yield self.grabFrame() except: break finally: self.cleanup() def cleanup(self): if self.rpiCam: self.camera.close() else: self.rawCapture.release()
###initialize a camera cam = PiCamera() cap1 = PiRGBArray(cam) ####################################### ####################################### ##take the time to initilize sleep(8) ###read the image cam.capture(cap1, format="bgr") image = cap1.array ####################################### ####################################### ###Do the processing #############df########################## ####################################### ### show the image cv2.imshow('image', image) ####################################### ####################################### ### close and exit cap1.release() cv2.waitKey(0) cv2.destroyAllWindows() #######################################
for i in range(5): df3=pandas.read_csv(lis[i]+".csv") x=df3.Time y=df3.Moisture plt.figure(i) plt.plot(x, y) plt.xlabel('TIME') plt.ylabel('MOISTURE CONTENT') plt.title(lis[i]) plt.savefig(lis[i]+'.jpg',bbox_inches='tight') for i in range(5): df3=pandas.read_csv(lis[i]+".csv") x=df3.Time y=df3.Moisture plt.plot(x, y) plt.xlabel('TIME') plt.ylabel('MOISTURE CONTENT') if i==4: plt.title('Blue-e501 Orange-e502 Green-e503 Red-e504 Violet-e505') if i==4: plt.savefig('mixed_graph.jpg',bbox_inches='tight') print("Graphs made!!!") if cv2.waitKey(3) & 0xFF == ord('q'): cap.release() cv2.destroyAllWindows break
faceCascade = cv2.CascadeClassifier( 'Cascades/haarcascade_frontalface_default.xml') cam = PiCamera() # shorten function to variable cam cam.resolution = (640, 480) #Small Resolution sutiable for cam cam.framerate = 40 # Framerate speed that the Pi-4 can handle appropriately rawCap = PiRGBArray( cam, size=(640, 480)) # Raw Image data in Array format so OPENCV can use # Size is passed in to match resolution #Sensor warmup sleep(1) for frame in cam.capture_continuous(rawCap, format="bgr", use_video_port=True): img = frame.array gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(20, 20)) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_gray = gray[y:y + h, x:x + w] roi_color = img[y:y + h, x:x + w] cv2.imshow('video', img) rawCap.truncate(0) key = cv2.waitKey(1) & 0xff if key == ord('q'): # press 'q' to quit break rawCap.release() cv2.destroyAllWindows()