-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
387 lines (334 loc) · 11.7 KB
/
main.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import cv2 as cv
from math import atan2, cos, sin, sqrt, pi
import numpy as np
import snap7
from snap7.util import *
from snap7.types import *
import time
def ReadMemory(plc, byte, bit, datatype): # define read memory function
result = plc.read_area(Areas['MK'], 0, byte, datatype)
if datatype == S7WLBit:
return get_bool(result, 0, 1)
elif datatype == S7WLByte or datatype == S7WLWord:
return get_int(result, 0)
elif datatype == S7WLReal:
return get_real(result, 0)
elif datatype == S7WLDWord:
return get_dword(result, 0)
else:
return None
def WriteMemory(plc, byte, bit, datatype, value): # define write memory function
result = plc.read_area(Areas['MK'], 0, byte, datatype)
if datatype == S7WLBit:
set_bool(result, 0, bit, value)
elif datatype == S7WLByte or datatype == S7WLWord:
set_int(result, 0, value)
elif datatype == S7WLReal:
set_real(result, 0, value)
elif datatype == S7WLDWord:
set_dword(result, 0, value)
plc.write_area(Areas['MK'], 0, byte, result)
def WriteValues(xvall, yvall, thetavall, xadress, yadress, thetaadress): # define write memory to specific adress function
WriteMemory(plc, 2, 0, S7WLBit, True) # write m2.0 True
WriteMemory(plc, xadress, 0, S7WLReal, xvall) # write x value to x adress
WriteMemory(plc, yadress, 0, S7WLReal, yvall) # write y value to y adress
WriteMemory(plc, thetaadress, 0, S7WLReal, thetavall) # write theta value to theta adress
WriteMemory(plc, 2, 0, S7WLBit, False) # write m2.0 False
IP = '192.168.0.1' # IP plc
RACK = 0 # RACK PLC
SLOT = 1 # SLOT PLC
plc = snap7.client.Client() # call snap7 client function
plc.connect(IP, RACK, SLOT) # connect to plc
state = plc.get_cpu_state() # read plc state run/stop/error
print(f'State:{state}') # print state plc
# Load the image
cap = cv.VideoCapture(0)
x = 0
y = 0
xbuffer = []
ybuffer = []
xsend = 0
ysend = 0
while True:
ret, frame = cap.read()
img = frame
readpermission = ReadMemory(plc, 10, 0, S7WLWord) # read mw10.0
WriteMemory(plc, 90, 0, S7WLReal, 1) # write camera connection value 1
# Was the image there?
if img is None:
print("Error: File not found")
exit(0)
# Convert image to grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Convert image to binary
_, bw = cv.threshold(gray, 110, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
# Find all the contours in the thresholded image
contours, _ = cv.findContours(bw, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
if readpermission == 1:
for i, c in enumerate(contours):
# Calculate the area of each contour
area = cv.contourArea(c)
# Ignore contours that are too small or too large
if area < 25000 or 100000 < area:
continue
# cv.minAreaRect returns:
# (center(x, y), (width, height), angle of rotation) = cv2.minAreaRect(c)
rect = cv.minAreaRect(c)
box = cv.boxPoints(rect)
box = np.int0(box)
# Retrieve the key parameters of the rotated bounding box
center = (int(rect[0][0]), int(rect[0][1]))
width = int(rect[1][0])
height = int(rect[1][1])
angle = float(rect[2])
if width < height:
angle = 90 - angle
else:
angle = -angle
label = " Rotation Angle: " + str(angle) + " degrees"
textbox = cv.rectangle(img, (center[0] - 35, center[1] - 25),
(center[0] + 295, center[1] + 10), (255, 255, 255), -1)
cv.putText(img, label, (center[0] - 50, center[1]),
cv.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 1, cv.LINE_AA)
cv.drawContours(img, [box], 0, (0, 0, 255), 2)
kx = 0.67757 # calibration for x
ky = 0.66298 # calibration for y
x = center[0] * kx
y = center[1] * ky
xbuffer.append(x)
ybuffer.append(y)
if len(xbuffer) >= 20:
xsend = sum(xbuffer) / len(xbuffer)
ysend = sum(ybuffer) / len(ybuffer)
print("**************************")
print("X:", xsend)
print("Y:", ysend)
print("ANGLE:", angle)
WriteValues(xsend, ysend, angle, 60, 70, 80)
xbuffer = []
ybuffer = []
else:
xbuffer = []
ybuffer = []
cv.imshow('Output Image', img)
key = cv.waitKey(1)
if key == 27:
break
WriteMemory(plc, 90, 0, S7WLReal, 0) # write camera connection value 0
cap.release()
cv.destroyAllWindows()
# Save the output image to the current directory
#cv.imwrite("min_area_rec_output.jpg", img)
###########################################################################################################
# while True:
# ret, frame = cap.read()
# cv2.imshow("fraame", frame)
# cv2.waitKey(1)
#
# cap.release()
# cv2.destroyAllWindows()
###########################################################################################################
########################################################## ana yedek:
# import numpy as np
# import matplotlib.pyplot as plt
# import cv2
#
# import snap7
# from snap7.util import *
# from snap7.types import *
# import time
#
# def ReadMemory(plc, byte, bit, datatype): # define read memory function
# result = plc.read_area(Areas['MK'], 0, byte, datatype)
# if datatype == S7WLBit:
# return get_bool(result, 0, 1)
# elif datatype == S7WLByte or datatype == S7WLWord:
# return get_int(result, 0)
# elif datatype == S7WLReal:
# return get_real(result, 0)
# elif datatype == S7WLDWord:
# return get_dword(result, 0)
# else:
# return None
#
#
# def WriteMemory(plc, byte, bit, datatype, value): # define write memory function
# result = plc.read_area(Areas['MK'], 0, byte, datatype)
# if datatype == S7WLBit:
# set_bool(result, 0, bit, value)
# elif datatype == S7WLByte or datatype == S7WLWord:
# set_int(result, 0, value)
# elif datatype == S7WLReal:
# set_real(result, 0, value)
# elif datatype == S7WLDWord:
# set_dword(result, 0, value)
# plc.write_area(Areas['MK'], 0, byte, result)
#
#
# def WriteValues(xvall, yvall, thetavall, xadress, yadress, thetaadress): # define write memory to specific adress function
# WriteMemory(plc, 2, 0, S7WLBit, True) # write m2.0 True
# WriteMemory(plc, xadress, 0, S7WLReal, xvall) # write x value to x adress
# WriteMemory(plc, yadress, 0, S7WLReal, yvall) # write y value to y adress
# WriteMemory(plc, thetaadress, 0, S7WLReal, thetavall) # write theta value to theta adress
# WriteMemory(plc, 2, 0, S7WLBit, False) # write m2.0 False
#
#
# IP = '192.168.0.1' # IP plc
# RACK = 0 # RACK PLC
# SLOT = 1 # SLOT PLC
#
# plc = snap7.client.Client() # call snap7 client function
# plc.connect(IP, RACK, SLOT) # connect to plc
#
# state = plc.get_cpu_state() # read plc state run/stop/error
# print(f'State:{state}') # print state plc
#
#
#
# cap = cv2.VideoCapture(0)
# w2 = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
# h2 = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# print("w: ", w2)
# print("h: ", h2)
#
# # TEMPLATE MATCHING METHOD
# template = cv2.imread("parcatip_ROBLAB.jpg", cv2.IMREAD_GRAYSCALE)
# w, h = template.shape[::-1]
#
# x = 0.0
# y = 0.0
# theta = 11
#
# while True:
# _, frame = cap.read()
#
# gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#
# res = cv2.matchTemplate(gray_frame, template, cv2.TM_CCOEFF_NORMED)
# loc = np.where(res >= 0.97)
#
# for pt in zip(*loc[::-1]):
# cv2.rectangle(frame, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 1)
# # Coordinates in mm
# k = 497/640 # 1px = 0.776mm
# # print("COORDINATES OF CENTER IN mm: ")
# # print("x: ", k*(pt[0]+w/2), "mm")
# # print("y: ", k*(pt[1]+h/2), "mm")
# x = k * (pt[0] + w / 2)
# y = k * (pt[1] + h / 2)
# time.sleep(0.5)
# print("*********************************************")
# print("x: ", x, "mm")
# print("y: ", y, "mm")
# readpermission = ReadMemory(plc, 10, 0, S7WLWord) # read mw10.0
# print('readpermission mw10:', readpermission)
#
# if readpermission == 1:
# # write memory
# WriteValues(x, y, theta, 60, 70, 80)
# # time.sleep(0.5)
#
# cv2.imshow("Frame", frame)
# key = cv2.waitKey(1)
#
#
#
# if key == 27:
# break
#
# cap.release()
# cv2.destroyAllWindows()
########################################################################################################## YEDEKKK X Y THETA plc yok
# import cv2 as cv
# from math import atan2, cos, sin, sqrt, pi
# import numpy as np
#
# # Load the image
# cap = cv.VideoCapture(0)
#
#
# x = 0
# y = 0
# xbuffer = []
# ybuffer = []
# xsend = 0
# ysend = 0
#
# while True:
# ret, frame = cap.read()
# img = frame
#
# # Was the image there?
# if img is None:
# print("Error: File not found")
# exit(0)
#
# # Convert image to grayscale
# gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#
# # Convert image to binary
# _, bw = cv.threshold(gray, 50, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
#
# # Find all the contours in the thresholded image
# contours, _ = cv.findContours(bw, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
#
# for i, c in enumerate(contours):
#
# # Calculate the area of each contour
# area = cv.contourArea(c)
#
# # Ignore contours that are too small or too large
# if area < 3700 or 100000 < area:
# continue
#
# # cv.minAreaRect returns:
# # (center(x, y), (width, height), angle of rotation) = cv2.minAreaRect(c)
# rect = cv.minAreaRect(c)
# box = cv.boxPoints(rect)
# box = np.int0(box)
#
# # Retrieve the key parameters of the rotated bounding box
# center = (int(rect[0][0]), int(rect[0][1]))
# width = int(rect[1][0])
# height = int(rect[1][1])
# angle = int(rect[2])
#
# if width < height:
# angle = 90 - angle
# else:
# angle = -angle
#
# label = " Rotation Angle: " + str(angle) + " degrees"
# textbox = cv.rectangle(img, (center[0] - 35, center[1] - 25),
# (center[0] + 295, center[1] + 10), (255, 255, 255), -1)
# cv.putText(img, label, (center[0] - 50, center[1]),
# cv.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 1, cv.LINE_AA)
# cv.drawContours(img, [box], 0, (0, 0, 255), 2)
# kx = 0.7795
# ky = 0.7826
# x = center[0]*kx
# y = center[1]*ky
# xbuffer.append(x)
# ybuffer.append(y)
# xsend = sum(xbuffer) / len(xbuffer)
# ysend = sum(ybuffer) / len(ybuffer)
#
# print("X:", x)
# print("Y:",y)
# #
# # if len(xbuffer) >= 70:
# # WriteValues(xsend, ysend, theta, 60, 70, 80)
# # xbuffer = []
# # ybuffer = []
#
# cv.imshow('Output Image', img)
# key = cv.waitKey(1)
# if key == 27:
# break
#
# cap.release()
# cv.destroyAllWindows()
#
#
# # Save the output image to the current directory
# #cv.imwrite("min_area_rec_output.jpg", img)