summaryrefslogtreecommitdiff
path: root/test-cli/test/helpers/detect.py
blob: 193dabf187d7321b1f964d054327c1779afdbbef (plain)
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
import cv2
import numpy as np


class Detect_Color(object):
    oFrame = None
    img_hsv = None
    __red_lower1 = [0, 50, 20]
    __red_upper1 = [5, 255, 255]
    __red_lower2 = [175, 50, 20]
    __red_upper2 = [180, 255, 255]
    __blue_lower = [110, 50, 50]
    __blue_upper = [130, 255, 255]
    __green_lower = [36, 25, 25]
    __green_upper = [86, 255, 255]

    def __init__(self, frame):
        self.oFrame = frame
        self.__hist()

    def __hist(self):
        self.img_hsv = cv2.cvtColor(self.oFrame, cv2.COLOR_BGR2HSV)

    def getRed(self):
        return self.__detect_two_areas([0, 50, 20], [5, 255, 255], [175, 50, 20], [180, 255, 255])

    def getBlue(self):
        return self.__detect_one_area([110, 50, 50], [130, 255, 255])

    def getGreen(self):
        return self.__detect_one_area([36, 25, 25], [86, 255, 255])

    def __detect_one_area(self, lower, upper):
        a_lower = np.array(lower)
        a_upper = np.array(upper)
        c_mask = cv2.inRange(self.img_hsv, a_lower, a_upper)
        croped = cv2.bitwise_and(self.oFrame, self.oFrame, mask=c_mask)
        mean_v = cv2.mean(self.oFrame, mask=c_mask)
        mean = {}
        mean['R'] = mean_v[2]
        mean['G'] = mean_v[1]
        mean['B'] = mean_v[0]
        count = cv2.countNonZero(c_mask)
        return c_mask, croped, mean, count

    def __detect_two_areas(self, lower1, upper1, lower2, upper2):
        a1_lower = np.array(lower1)
        a1_upper = np.array(upper1)
        a2_lower = np.array(lower2)
        a2_upper = np.array(upper2)
        c_mask1 = cv2.inRange(self.img_hsv, a1_lower, a1_upper)
        c_mask2 = cv2.inRange(self.img_hsv, a2_lower, a2_upper)
        c_mask = cv2.bitwise_or(c_mask1, c_mask2)
        croped = cv2.bitwise_and(self.oFrame, self.oFrame, mask=c_mask)
        mean_v = cv2.mean(self.oFrame, mask=c_mask)
        mean = {}
        mean['R'] = mean_v[2]
        mean['G'] = mean_v[1]
        mean['B'] = mean_v[0]
        count = cv2.countNonZero(c_mask)
        return c_mask, croped, mean, count