-
Notifications
You must be signed in to change notification settings - Fork 1
/
06_picam_video.py
78 lines (52 loc) · 2.67 KB
/
06_picam_video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
# This program illustrates how to capture frames in a video stream and how to do further processing on them
# It uses numpy to do the calculations and OpenCV to display the frames
import picamera
import picamera.array # This needs to be imported explicitly
import time
import cv2
import numpy as np
# Initialize the camera
camera = picamera.PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
camera.vflip = False # Flip upside down or not
camera.hflip = True # Flip left-right or not
# Create a data structure to store a frame
rawframe = picamera.array.PiRGBArray(camera, size=(640, 480))
# Allow the camera to warm up
time.sleep(0.1)
if __name__ == '__main__':
try:
# Continuously capture frames from the camera
# Each rawframe is accessible as ´frame´ inside the for-loop
# Note that the format is BGR instead of RGB because we want to use openCV later on and it only supports BGR
for frame in camera.capture_continuous(rawframe, format = 'bgr', use_video_port = True):
# Clear the rawframe in preparation for the next frame
# Do not modify this line of code
rawframe.truncate(0)
# Create a numpy array representing the image
# Do not modify this line of code
image = frame.array
#-----------------------------------------------------
# We will use numpy to do our image manipulations
#-----------------------------------------------------
# Make a copy of the image
image2 = image.copy()
image2.setflags(write=1) # Making the array mutable
# Modify the copy of the image
# This is where you would write your code to manipulate the image (invert it, make it grayscale, etc.)
# Remember: images are stored as BGR
w,h,d = image2.shape
image2[w//4:3*w//4 , h//4:3*h//4 , :] = 255 - image2[w//4:3*w//4 , h//4:3*h//4 , :]
# Show the frames
cv2.imshow("Orignal frame", image)
cv2.imshow("Modified frame", image2)
# The waitKey command is needed to force openCV to show the image
cv2.waitKey(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Program stopped by User")
cv2.destroyAllWindows()
# Clean up the camera resources
camera.close()