-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner_test.py
More file actions
177 lines (146 loc) · 4.96 KB
/
scanner_test.py
File metadata and controls
177 lines (146 loc) · 4.96 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
168
169
170
171
172
173
174
175
176
177
# import packages
import numpy as np
import cv2
import imutils
import argparse
from skimage.filters import threshold_local
from transformations import *
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import torch
from detecto import core, utils, visualize
from detecto.visualize import show_labeled_image, plot_prediction_grid
from torchvision import transforms
import numpy as np
def midpoint(ptA, ptB,ptC,ptD):
return ((ptA + ptB) * 0.5, (ptC + ptD) * 0.5)
def click_event(event, x, y, flags, params):
# checking for left mouse clicks
if event == cv2.EVENT_LBUTTONDOWN:
# displaying the coordinates
# on the Shell
print(x, ' ', y)
# displaying the coordinates
# on the image window
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image, str(x) + ',' +
str(y), (x,y), font,
1, (255, 0, 0), 2)
cv2.imshow('image', image)
# checking for right mouse clicks
if event==cv2.EVENT_RBUTTONDOWN:
# displaying the coordinates
# on the Shell
print(x, ' ', y)
# displaying the coordinates
# on the image window
font = cv2.FONT_HERSHEY_SIMPLEX
b = image[y, x, 0]
g = image[y, x, 1]
r = image[y, x, 2]
cv2.putText(image, str(b) + ',' +
str(g) + ',' + str(r),
(x,y), font, 1,
(255, 255, 0), 2)
cv2.imshow('image', image)
model = core.Model.load('model_weights.pth', ['Duck', 'Red Chips', 'Green Chips','Green Pedestal','Green Pedestal_Tilt','Red Pedestal','Red Pedestal_Tilt','Duck_Left','Duck_Right','Duck_Front','Duck_Back'])
# construct argument parser and parse args
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', default=r'C:\Users\estde\Documents\Object Detection\Out.jpg')
args = vars(ap.parse_args())
# load image, compute the ratio of old height
# to the new height, clone and resize
image = cv2.imread(r'C:\Users\estde\Documents\Object Detection\20221101_182957.jpg')
ratio = image.shape[0] / 500.0
original = image.copy()
image = imutils.resize(image, height=500)
# convert image to gray, blur and apply edge detection
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5,5), 0)
edged = cv2.Canny(image, 150, 200)
# show original image and edge detected image
print('applying edge detection')
cv2.imshow('Original Image', image)
cv2.imshow('Edged', edged)
cv2.waitKey(0)
#cv2.destroyAllWindows()
# find contours and keep largest one
# initialize screen contour
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
# loop through contours
for c in cnts:
# approximate contours
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02*peri, True)
# check if approximated contour has four points
if len(approx) == 4:
screen_contours = approx
break
else:
print('Contours not detected')
break
# show contour outline of document
print('finding contours of document')
cv2.drawContours(image, [screen_contours], -1, (255,0,0), 2)
image = imutils.resize(image, width=image.shape[0])
cv2.imshow('Document Outline', image)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
# apply transform
warped = perspective_transform(original, screen_contours.reshape(4,2)* ratio)
# convert to grayscale then threshold it
#warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
#T = threshold_local(warped, 11, offset=10, method='gaussian')
#warped = (warped>T).astype('uint8') * 255
# show original image and scanned images
print('applying perspective transform')
name = args['image'][:len(args['image'])-4]
filename = name+'_scanned.jpg'
cv2.imwrite(filename, warped)
print('saved scan: {}'.format(filename))
image2 = cv2.imread(r'C:\Users\estde\Documents\Object Detection\Out_scanned.jpg')
predictions = model.predict(image2)
labels, boxes, scores = predictions
thresh=0.5
filtered_indices=np.where(scores>thresh)
filtered_scores=scores[filtered_indices]
filtered_boxes=boxes[filtered_indices]
num_list = filtered_indices[0].tolist()
filtered_labels = [labels[i] for i in num_list]
newwarp = show_labeled_image(image2,filtered_boxes,filtered_labels)
objs = []
for i in filtered_boxes:
print( "i = ")
print(i)
j = 0
t = 0
index1 = 0
labelstr = ''
while j < 4:
if(j == 0):
x = i[j]
print("yep1")
if(j == 1):
y = i[j]
print("yep2")
if(j == 2):
w = i[j]
print("yep3")
if(j == 3):
h = i[j]
print("yep4")
print( "j = ")
print(j)
j = j + 1
objs.append(midpoint(x,y,w,h))
print(objs[0])
#plt.plot((0,0),objs[0])
img = plt.imread(filename)
plt.imshow(img)
cv2.imshow('Original', imutils.resize(original, height=650))
cv2.imshow('Scanned Document', imutils.resize(warped, height=650))
# setting mouse handler for the image
# and calling the click_event() functio
cv2.waitKey(0)