-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfUtil_Bar.py
More file actions
78 lines (61 loc) · 3.01 KB
/
cfUtil_Bar.py
File metadata and controls
78 lines (61 loc) · 3.01 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
import cv2
import numpy as np
class BarDetectorC:
def __init__(self, shape):
self.h, self.w, self.c = shape
self.areaMin = 30
self.areaMax = 500
self.lowerColor = np.array([29, 85, 85])
self.upperColor = np.array([38, 255, 255])
def FindBar(self, img, bb):
# Convert image to HSV and filter out all pixels outside desired color range
hsv = cv2.cvtColor(bb, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, self.lowerColor, self.upperColor)
# Detect Object and stor position and metrics
marks = []
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Get position and size perimeters of objects
for contour in contours:
area = cv2.contourArea(contour)
if area > self.areaMin and area < self.areaMax:
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
x, y, w, h = cv2.boundingRect(approx)
marks.append([x, y, w, h])
# Mark object with contour line and rectangle and write specs
cv2.drawContours(mask, contour, -1, (255, 0, 255), 7)
cv2.rectangle(mask, (x, y), (x + w, y + h), (0, 0, 255), 3)
cv2.putText(mask, "Points:" + str(len(approx)), (x + w + 20, y + 20), cv2.FONT_HERSHEY_PLAIN, 1,
(255, 255, 255), 2)
cv2.putText(mask, "Area:" + str(int(area)), (x + w + 20, y + 45), cv2.FONT_HERSHEY_PLAIN, 1,
(255, 255, 255), 2)
# Draw line between the two markers
y = -1
barFound = 0
if len(marks) == 2:
x1, y1, x2, y2 = (marks[0][0] + int(0.5 * marks[0][2]), marks[0][1] + int(0.5 * marks[0][3]),
marks[1][0] + int(0.5 * marks[1][2]), marks[1][1] + int(0.5 * marks[1][3]))
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
x, y = int((x1 + x2)/2), int(self.h - ((y1+y2)/2))
barFound = 1
cv2.putText(img, f'{y}', (x, int((y1+y2)/2)-50), cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 0, 0), 1)
cv2.line(bb, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.putText(bb, f'{y}', (x, int((y1+y2)/2)-50), cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 0, 0), 1)
# Change the dimensions of the mask so that it may be stores in th evideo writer object in main
mask = cv2.cvtColor(mask, cv2.COLOR_BAYER_GR2BGR)
return barFound, y, mask
class BarDetectorDL:
def __init__(self, file):
self.model = cv2.CascadeClassifier(file)
def BarDrawRectangle(self, img, rectangle, single=False):
# Static Variables
color = (0, 0, 255)
for (x, y, w, h) in rectangle:
# Calculate corners
p1 = (x, y)
p2 = (x + w, y + h)
# Draw
cv2.rectangle(img, p1, p2, color, 2)
if single:
cv2.imshow('Bars', img)
cv2.waitKey(0)