-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_or_capture.py
More file actions
167 lines (122 loc) · 5.65 KB
/
read_or_capture.py
File metadata and controls
167 lines (122 loc) · 5.65 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -----------------------------------------------------------------------------------------------
### Imports ###
# -----------------------------------------------------------------------------------------------
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from imutils import rotate_bound
# -----------------------------------------------------------------------------------------------
### Greetings ###
# -----------------------------------------------------------------------------------------------
def greetings():
print(
"""Greetings!!! Before you proceed to use this application please note the following --
1. Since the A4 paper is white, placing white objects on the paper will
most likely produce faulty outcome.
2. Place only one object on the A4 paper at a time. To reduce false detection, only the highest
perimeter object is filtered from all the possible detections.
3. This application only uses OpenCV and no deep learning model. So, due
to constraints of classical computer vision the results may not be
100% accurate. Long story short, Use at your own risk.
To improve the chance of correct recognition and measurement you can do the following --
1. Capture the image in a clean background.
2. Try to fit the whole A4 paper inside the frame.
3. Lighting condition should not be too dark or too bright."""
)
# -----------------------------------------------------------------------------------------------
### Read Image ###
# -----------------------------------------------------------------------------------------------
def read_image(img_path):
try:
img = cv.imread(img_path)
return img
except:
print("Invalid Image Path")
# -----------------------------------------------------------------------------------------------
### Capture Live Image ###
# -----------------------------------------------------------------------------------------------
def take_picture(device_id=0):
cap = cv.VideoCapture(device_id)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, 720)
cap.set(cv.CAP_PROP_FRAME_WIDTH, 700)
try:
if cap.isOpened() is False:
cap.open()
except:
print("Maybe try with another device. Exiting...\n")
return None
while True:
ret, frame = cap.read()
if ret is False:
print("Could not read any frame. Exiting.... \n")
break
cv.imshow("To capture an image, press 'c' and to quit, press 'q'", frame)
key_pressed = cv.waitKey(1) # wait 1 mili second for key press
if key_pressed == ord("c"):
captured_image = frame
break
if key_pressed == ord("q"):
captured_image = None
print("No image was captured. Quitting..... \n")
break
cap.release()
cv.destroyAllWindows()
return captured_image
# -----------------------------------------------------------------------------------------------
### User Input ###
# -----------------------------------------------------------------------------------------------
def usr_prompt():
"""
Prompt the user to provide a valid image path or a valid device id to capture one.
Returns: User input
"""
try:
img_path_or_capture = int(
input(
"Please select \n 1. to provide an image path or, 2. to take a live picture \n (1, 2): \t"
)
)
except:
raise
print(f"img_path_or_capture: {img_path_or_capture}")
if img_path_or_capture not in [1, 2]:
print("Invalid input. Please choose either 1 or 2.")
if img_path_or_capture == 1:
img_path = input("Please provide a valid image path : \t")
return img_path
if img_path_or_capture == 2:
video_capture_device_id = input(
"Please provide a device id (to capture the image with, default is 0): \t"
)
if video_capture_device_id == "":
video_capture_device_id = 0
try:
video_capture_device_id = int(video_capture_device_id)
return video_capture_device_id
except:
raise
# -----------------------------------------------------------------------------------------------
### Read or Capture Image ###
# -----------------------------------------------------------------------------------------------
def read_or_capture(prompt_usr=True, img_path=None, device_id=None):
greetings()
if prompt_usr:
usr_input = usr_prompt()
if type(usr_input) == str:
img = read_image(usr_input)
if type(usr_input) == int:
img = take_picture(usr_input)
if img is None:
print("Please capture another image or provide a valid image path. \n")
return None
if not prompt_usr and img_path:
img = read_image(img_path)
if not prompt_usr and device_id is not None:
img = take_picture(device_id)
if img is None:
print("Please capture another image or provide a valid image path. \n")
return None
# rotate the image by 90 degree CCW if image width > image height
if img.shape[0] < img.shape[1]:
img = rotate_bound(img, -90)
return img