-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtip.py
More file actions
92 lines (83 loc) · 3.16 KB
/
tip.py
File metadata and controls
92 lines (83 loc) · 3.16 KB
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
import cv2
import pylab
from scipy import ndimage
import time
import configparser
import argparse
## load config
config = configparser.ConfigParser()
config_ini = "config.ini"
config.read(config_ini)
debug = config.get("SETTINGS", "debug")
frames = config.getint("SETTINGS", "frames")
camera = cv2.VideoCapture(0)
img_path = ("images/tip/")
## check for args passed
parser = argparse.ArgumentParser()
parser.add_argument('--roi',type=str, required=False)
parser.add_argument('--show',type=str, required=False)
parser.add_argument('--capture', type=str, required=False)
args = parser.parse_args()
def edit_config(setting, parameter, value):
config.set(setting, parameter, value)
with open(config_ini, "w") as config_file:
config.write(config_file)
def capture_frames(frames):
if debug:
st = time.time()
frame = 0
while frame < frames: # loop until taken enough frames
frame += 1
result, image = camera.read() # read from camera
cv2.imwrite(f"{img_path}{frame}.jpg", image) # save images
if debug:
print(f"{frame} frames captured")
et = time.time()
elapsed_time = et - st
print('Execution time (capture):', elapsed_time, 'seconds')
def set_roi():
merged_all = cv2.imread(f'{img_path}1.jpeg')
roi = cv2.selectROI(merged_all) # select roi
roi_cropped = merged_all[int(roi[1]):int(roi[1]+roi[3]), int(roi[0]):int(roi[0]+roi[2])] # crop image
edit_config("TIP_ROI", "y1", str(roi[1]))
edit_config("TIP_ROI", "y2", str(roi[1]+roi[3]))
edit_config("TIP_ROI", "x1", str(roi[0]))
edit_config("TIP_ROI", "x2", str(roi[0]+roi[2]))
cv2.imwrite(f"{img_path}roi_cropped.jpg",roi_cropped) # save cropped image
def get_roi():
merged_all = cv2.imread(f'{img_path}1.jpeg')
y1 = config.getint("TIP_ROI", "y1")
y2 = config.getint("TIP_ROI", "y2")
x1 = config.getint("TIP_ROI", "x1")
x2 = config.getint("TIP_ROI", "x2")
roi_cropped = merged_all[y1:y2, x1:x2] # crop image
cv2.imwrite(f"{img_path}roi_cropped.jpg",roi_cropped) # save cropped image
def get_height():
im = cv2.imread(f'{img_path}roi_cropped.jpg')
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # convert to gray
threshold = config.getint("TIP_SETTING", "threshold")
#print('height, width:', gray.shape) #height and width of image
ret, thresholded = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY)
x, y, width, height = cv2.boundingRect(thresholded)
text = f"Height: {height} Width: {width}"
text_position = (5,15)
cv2.putText(thresholded, text, org, fontFace = cv2.FONT_HERSHEY_SIMPLEX, fontScale = 0.5, color = (250,225,100))
cv2.imwrite(f"{img_path}thresholded.jpg",thresholded)
cv2.imwrite(f"{img_path}grayscale.jpg",gray)
def show_images():
im = cv2.imread(f'{img_path}1.jpeg')
roi_cropped = cv2.imread(f'{img_path}roi_cropped.jpg')
thresholded = cv2.imread(f'{img_path}thresholded.jpg')
cv2.imshow("1st Frame", im)
cv2.imshow("ROI cropped", roi_cropped)
cv2.imshow("thresholded", thresholded)
cv2.waitKey(0)
if args.capture:
capture_frames(frames)
if args.roi:
set_roi()
else:
get_roi()
get_height()
if args.show:
show_images()