-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetection.py
More file actions
80 lines (60 loc) · 2.9 KB
/
detection.py
File metadata and controls
80 lines (60 loc) · 2.9 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
import cv2
import numpy as np
import time
from pyzbar import pyzbar
#from yolov4 import Detector
blueLower = (120,100,20)
blueUpper = (179,255,255)
def imageprocessing(imgOriginal):
blurred = cv2.GaussianBlur(imgOriginal, (11, 11), 0)
# hsv
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
# mavi için maske oluştur
mask = cv2.inRange(hsv, blueLower, blueUpper)
# maskenin etrafında kalan gürültüleri sil
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# farklı sürüm için
# (_, contours,_) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# kontur
(contours, _) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
center = None
x1,x2,y1,y2 = int(imgOriginal.shape[1]*0.25),int(imgOriginal.shape[1]*0.75),int(imgOriginal.shape[0]*0.10),int(imgOriginal.shape[0]*0.90)
cv2.rectangle(imgOriginal, (x1, y1), (x2, y2), (0, 0, 255), 2)
if len(contours) > 0:
return _extracted_from_imageprocessing_20(contours, imgOriginal)
else:
return 0,0,0,0,0,0,imgOriginal
# TODO Rename this here and in `imageprocessing`
def _extracted_from_imageprocessing_20(contours, imgOriginal):
# en buyuk konturu al
c = max(contours, key=cv2.contourArea) # alanı en buyuk olan konturu al
# dikdörtgene çevir
x_c,y_c,w_c,h_c = cv2.boundingRect(c)# bu alanı kapsayacak en kucuk dıkdortgenı cız
rect=cv2.minAreaRect(c)
((x, y), (width, height), rotation) = rect
#s = "x: {}, y: {}, width: {}, height: {}, rotation: {}".format(np.round(x), np.round(y), np.round(width),
# np.round(height), np.round(rotation))
# print(s)
# kutucuk
box = cv2.boxPoints(rect)
box = np.int64(box) # nesnenın etrafını kaplıcagımız kutucuk
# moment
M = cv2.moments(c) # merkez noktayı bulmamızı saglıyan sey
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # kutucugun orta noktası
# konturu çizdir: sarı
cv2.drawContours(imgOriginal,[box],0,(0,255,255),2)
#cv2.rectangle(imgOriginal, (x,y),(x+w,y+h), (0, 255, 255), 2) # kutucugu cızdırıyoruz orjinal frame e
# merkeze bir tane nokta çizelim: pembe
cv2.circle(imgOriginal, center, 5, (255, 0, 255), -1)
# bilgileri ekrana yazdır
#cv2.putText(imgOriginal, s, (25, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), 2)
return 1,center,x_c,y_c,w_c,h_c,imgOriginal
def qr_detection(imgOriginal):
qr_code_list = pyzbar.decode(imgOriginal)
x1, x2, y1, y2 = int(imgOriginal.shape[1] * 0.25), int(imgOriginal.shape[1] * 0.75), int(
imgOriginal.shape[0] * 0.10), int(imgOriginal.shape[0] * 0.90)
cv2.rectangle(imgOriginal, (x1, y1), (x2, y2), (0, 0, 255), 2)
for qr_code in qr_code_list:
return qr_code.data,imgOriginal
return 0,imgOriginal