-
Notifications
You must be signed in to change notification settings - Fork 2
/
take_photo.py
70 lines (54 loc) · 1.46 KB
/
take_photo.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
import numpy as np
import cv2
import time
"""
Take A Photo
This script uses OpenCV to take a picture
from a webcam or USB camera device.
"""
def take_photo(camera_port=0):
"""
Captures an image from a web camera using OpenCV
"""
camera = cv2.VideoCapture(camera_port)
print("Say cheese")
time.sleep(3)
# Wait, otherwise the image will be over-dark
time.sleep(0.3)
return_value, image = camera.read()
# Make sure you show the image
# before the camera is deleted,
# otherwise you will get a segfault
imageT = transpose_photo(image)
show_image('cam', image)
show_image('camT', imageT)
print("Done")
del(camera)
return image
def transpose_photo(image):
"""
Transposes the first two axes of an image (diagonal reflection)
"""
imageT = np.transpose(image, axes=(1,0,2))
return imageT
def save_image(filename, image):
"""
Saves an image array to a file
"""
cv2.imwrite(filename, image)
def show_image(windowname, image):
"""
Show the image in a new window using OpenCV imshow
"""
# Make the window resizable
cv2.namedWindow('cam', cv2.WINDOW_NORMAL)
cv2.imshow('cam',image)
cv2.waitKey()
if __name__=="__main__":
# Take
# (Change this to 0 to use built-in webcam, 1 to use USB webcam)
img = take_photo(0)
imgT = transpose_photo(img)
# Save
save_image("images/opencv.png", img)
save_image("images/opencvT.png", imgT)