Repository navigation
Expand file tree
/
Copy pathinference.py
More file actions
164 lines (116 loc) · 4.18 KB
/
Copy pathinference.py
File metadata and controls
164 lines (116 loc) · 4.18 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
"""
Inference script for digit recognition on a webcam or USB camera.
"""
import sys
from pickle import load
import cv2
import numpy as np
import pandas as pd
from skimage.color import rgb2gray
from skimage.filters import threshold_otsu
from skimage.measure import regionprops_table
from skimage.morphology import label
from skimage.transform import resize
from skimage.util import img_as_ubyte
from skimage.exposure import rescale_intensity
from skimage.segmentation import clear_border
from preprocessing import keep_n_biggest_objects
# Reload the sklearn `pipeline`
with open("pipeline.pkl", "rb") as f:
pipe = load(f)
def detect_digit_roi(image: np.ndarray, max_objects=12, min_area_px=2000) -> pd.DataFrame:
# RGB => Grayscale
gray = rgb2gray(image)
# Otsu threshold
binary = gray < threshold_otsu(gray)
# Labelling + keep biggest object
labelled = label(binary)
# Remove object touching the image border (incl. the white background, probably)
labelled = clear_border(labelled)
# Keep the biggest `max_objects` objects only
digit_crops = keep_n_biggest_objects(labelled, n=max_objects)
# Return a dataframe with intensity image and centroids
df = pd.DataFrame(
regionprops_table(
digit_crops,
intensity_image=gray,
properties=["area", "bbox"])
)
# Size filter (ignore small objects)
df = df[df["area"] > min_area_px]
digit_crops = []
for _, row in df.iterrows():
# Crop around the digit
digit_crop = gray[int(row["bbox-0"]) : int(row["bbox-2"]), int(row["bbox-1"]) : int(row["bbox-3"])]
# Resize to a fixed size
digit_crop = resize(digit_crop, output_shape=(50, 50))
# Convert to 8-bit
digit_crop = img_as_ubyte(digit_crop)
digit_crops.append(digit_crop)
df["roi"] = digit_crops
return df
def classify_digit_roi(digit: np.ndarray) -> str:
# Normalize intensity values
digit_normed = rescale_intensity(digit, out_range=(0, 1))
# Flatten pixels
pixel_features = np.reshape(digit_normed[None], (1, -1))
# Predict
pred_label = pipe.predict(pixel_features)[0]
return pred_label
def draw_boxes(img, df: pd.DataFrame, font_size=1.5, thickness=2):
boxes = df[['bbox-0', 'bbox-1', 'bbox-2', 'bbox-3']].values
labels = df["pred"].values
output = img.copy()
for i, box in enumerate(boxes):
y1, x1, y2, x2 = map(int, box)
cv2.rectangle(output, (x1, y1), (x2, y2), color=(0, 255, 0), thickness=thickness)
text = str(labels[i])
(text_w, text_h), baseline = cv2.getTextSize(
text=text,
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=font_size,
thickness=thickness,
)
text_y = y1 + text_h + 14
cv2.rectangle(
output,
(x1, text_y - text_h - baseline),
(x1 + text_w, text_y + baseline),
color=(0, 255, 0),
thickness=-1
)
cv2.putText(
img=output,
text=text,
org=(x1, text_y),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=font_size,
color=(0, 0, 0),
thickness=thickness,
lineType=cv2.LINE_AA,
)
return output
def main(camera_idx: int):
cap = cv2.VideoCapture(camera_idx)
if not cap.isOpened():
raise RuntimeError("Could not open camera")
cv2.namedWindow("Camera", cv2.WINDOW_NORMAL)
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
df = detect_digit_roi(frame)
df["pred"] = df["roi"].apply(classify_digit_roi)
vis_frame = draw_boxes(frame, df)
cv2.imshow("Camera", vis_frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
# Run with the correct camera index, for example: `python inference.py 4`
_, camera_idx = sys.argv
camera_idx = int(camera_idx)
main(camera_idx)