Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Detect_Img.py 5.2 KB

You have to be logged in to leave a comment. Sign In
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
  1. from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
  2. from tensorflow.keras.preprocessing.image import img_to_array
  3. from tensorflow.keras.models import load_model
  4. import numpy as np
  5. import cv2
  6. import os
  7. import glob
  8. IMAGE = "images"
  9. FACE = "face_detector"
  10. MODEL = "mask_detector.model"
  11. CONFIDENCE = 0.5
  12. # 辨識口罩顏色
  13. def calculate_mask_area(frame):
  14. hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  15. color = (0,'')
  16. # blue color
  17. low_blue = np.array([94, 80, 2])
  18. high_blue = np.array([126, 255, 255])
  19. blue_mask = cv2.inRange(hsv_frame, low_blue, high_blue)
  20. blue = cv2.bitwise_and(frame, frame, mask=blue_mask)
  21. blue_count = (cv2.countNonZero(blue_mask),'BLUE')
  22. color = (blue_count[0],blue_count[1])
  23. # Red color
  24. low_red = np.array([161, 155, 84])
  25. high_red = np.array([179, 255, 255])
  26. red_mask = cv2.inRange(hsv_frame, low_red, high_red)
  27. red = cv2.bitwise_and(frame, frame, mask=red_mask)
  28. red_count = (cv2.countNonZero(red_mask),'RED')
  29. if (red_count[0]>color[0]):
  30. color = (red_count[0],red_count[1])
  31. # Green color
  32. low_green = np.array([25, 52, 72])
  33. high_green = np.array([150, 255, 255])
  34. green_mask = cv2.inRange(hsv_frame, low_green, high_green)
  35. green = cv2.bitwise_and(frame, frame, mask=green_mask)
  36. green_count = (cv2.countNonZero(green_mask),'GREEN')
  37. if (green_count[0]>color[0]):
  38. color = (green_count[0],green_count[1])
  39. # White color
  40. low_white = np.array([0, 0, 230])
  41. high_white = np.array([360, 10, 255])
  42. white_mask = cv2.inRange(hsv_frame, low_white, high_white)
  43. white = cv2.bitwise_and(frame, frame, mask=white_mask)
  44. white_count = (cv2.countNonZero(white_mask),'WHITE')
  45. if (white_count[0]>color[0]):
  46. color = (white_count[0],white_count[1])
  47. # Black color
  48. low_black = np.array([0, 0, 0])
  49. high_black = np.array([360, 255, 15])
  50. black_mask = cv2.inRange(hsv_frame, low_black, high_black)
  51. black = cv2.bitwise_and(frame, frame, mask=black_mask)
  52. black_count = (cv2.countNonZero(black_mask),'BLACK')
  53. if (black_count[0]>color[0]):
  54. color = (black_count[0],black_count[1])
  55. return color
  56. def mask_image():
  57. # 每幾秒撥放一張
  58. persecond = 5000
  59. # 載入臉部偵測opencv model
  60. prototxtPath = os.path.sep.join([FACE, "deploy.prototxt"])
  61. weightsPath = os.path.sep.join([FACE, "res10_300x300_ssd_iter_140000.caffemodel"])
  62. net = cv2.dnn.readNet(prototxtPath, weightsPath)
  63. # 載入口罩偵測模型
  64. model = load_model(MODEL)
  65. img_list = glob.glob(IMAGE+'/*.*')
  66. for (idx, img) in enumerate(img_list) :
  67. image = cv2.imread(img)
  68. orig = image.copy()
  69. (h, w) = image.shape[:2]
  70. # 圖片前處理,資料標準化
  71. blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300),
  72. (104.0, 177.0, 123.0))
  73. net.setInput(blob)
  74. detections = net.forward()
  75. print(detections.shape)
  76. print(detections.shape[2])
  77. for i in range(0, detections.shape[2]):
  78. confidence = detections[0, 0, i, 2]
  79. # 取信賴程度>50%
  80. if confidence > CONFIDENCE:
  81. box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
  82. (startX, startY, endX, endY) = box.astype("int")
  83. (startX, startY) = (max(0, startX), max(0, startY))
  84. (endX, endY) = (min(w - 1, endX), min(h - 1, endY))
  85. # 將臉的畫面框起來
  86. face = image[startY:endY, startX:endX]
  87. mask_color = calculate_mask_area(face)
  88. # print(mask_color[1])
  89. # 辨識口罩顏色
  90. face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
  91. face = cv2.resize(face, (224, 224))
  92. face = img_to_array(face)
  93. face = preprocess_input(face)
  94. face = np.expand_dims(face, axis=0)
  95. (mask, withoutMask) = model.predict(face)[0]
  96. # 戴口罩顯示藍色、未戴口罩顯示紅色
  97. label = "Mask" if mask > withoutMask else "No Mask"
  98. color = (255, 0, 0) if label == "Mask" else (0, 0, 255)
  99. # 呈現機率
  100. label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
  101. # 將結果顯示在圖片上
  102. text = f'{mask_color[1]} - {label}' if mask > withoutMask else f"{label}"
  103. cv2.putText(image, text, (startX, startY - 10),
  104. cv2.FONT_HERSHEY_DUPLEX, 0.5, color, 2)
  105. cv2.rectangle(image, (startX, startY), (endX, endY), color, 2)
  106. # 顯示圖片
  107. cv2.imshow("Output", image)
  108. k = cv2.waitKey(persecond)
  109. # Esc: 27
  110. # Space: 32
  111. # Enter: 13
  112. if k in (27,13,32) :
  113. persecond = 0
  114. cv2.imshow("Output", image)
  115. cv2.destroyAllWindows()
  116. if __name__ == "__main__":
  117. mask_image()
  118. # %%
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...