-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
93 lines (75 loc) · 2.46 KB
/
camera.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# camera.py
"""
Provides support for Picamera v3. Limited functionality: used to find a blue
line and return error away from center of frame.
"""
import os
import sys
import time
import cv2
from libcamera import controls # type:ignore
from numpy import ndarray
from picamera2 import Picamera2 # type:ignore
__author__ = "Ben Kraft"
__copyright__ = "None"
__credits__ = "Ben Kraft"
__license__ = "MIT"
__version__ = "1.2"
__maintainer__ = "Ben Kraft"
__email__ = "[email protected]"
__status__ = "Prototype"
picam = Picamera2() # assigns camera variable
picam.set_controls({"AfMode": controls.AfModeEnum.Continuous}) # sets auto focus mode
picam.start() # activates camera
def main() -> None:
"""
Runs default library acions.
"""
time.sleep(1.5)
do_display = not "-d" in sys.argv
iteration = 0
# Acquires object from command line input
object = input("\nEnter an object to be sampled: ")
destination_path = os.path.join("Samples", object)
print(f"\nTaking picture samples for export in: [ {destination_path} ]\n")
try:
while True:
# Input advance
input("[Enter] to advance: ")
# Takes picture and converts to color space
image_bgr = take_picture(do_display)
# Makes directory if does not already exist
if not os.path.isdir(destination_path):
os.mkdir(destination_path)
# Creates a path for image to be written to
image_path = os.path.join(destination_path, f"sample_{iteration}.jpg")
cv2.imwrite(image_path, image_bgr)
print(f"Wrote image to: [ {image_path} ]")
iteration += 1
except KeyboardInterrupt:
print("")
def take_picture(display: bool = False) -> ndarray:
"""
Takes a picture and returns it as a numpy array.
"""
image_data = picam.capture_array("main")
# Converts the image from RGB to BGR format for displays
image_bgr = cv2.cvtColor(image_data, cv2.COLOR_RGB2BGR)
if display:
# Display the image
cv2.imshow("Raw Image", image_bgr)
cv2.waitKey(1)
return image_bgr
def crop(
image_data: ndarray, size: tuple[int, int], position: tuple[int, int]
) -> ndarray:
"""
Returns data cropped to size and at position.
"""
return image_data[
position[0] : (position[0] + size[0]),
position[1] : (position[1] + size[1]),
]
if __name__ == "__main__":
main()