File size: 4,616 Bytes
d6a7d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import cv2
import numpy as np
import requests
from mmdet.apis import init_detector, inference_detector
import mmcv
import torch
from mmdet.utils.contextmanagers import concurrent
from pprint import pprint
from PIL import Image
import datetime



# Specify the path to model config and checkpoint file
config_file = 'configs/fasterrcnn.py'
checkpoint_file = 'models/fasterrcnn.pth'

# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')

# test a single image and show the results
img = 'demo2.png'  # or img = mmcv.imread(img), which will only load it once
result = inference_detector(model, img)
# visualize the results in a new window
model.show_result(img, result)
# or save the visualization results to image files
model.show_result(img, result, out_file='result.jpg')

list_objects = []

for i in result[1]:
    temp = i
    temp = np.append(temp, 1)
    list_objects.append(temp)

for i in result[2]:
    temp = i 
    temp = np.append(temp, 2)
    list_objects.append(temp)

for i in result[3]:
    temp = i
    temp = np.append(temp, 3)
    list_objects.append(temp)

img = cv2.imread(img)
for i in list_objects:
    if i[5] == 1:
        color = (255, 0, 0)
        text  = "Mask weared incorrect"
    elif i[5] == 2:
        color = (0, 255, 0)
        text  = "With mask"
    elif i[5] == 3:
        color = (0, 0, 255)
        text = "Without mask"
    text += ": " + str(round(i[4], 2))
    x1 = i[0]
    y1 = i[1]
    x2 = i[2] - 1
    y2 = i[3] - 1

    x1 = round(x1)
    y1 = round(y1)
    x2 = round(x2)
    y2 = round(y2)

    img = cv2.rectangle(img, (x1, y1), (x2, y2), color, 3)
    img = cv2.putText(img, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
cv2.imwrite('Original_result.jpg', img)

def IoU(bbox1, bbox2):

    x1_left = bbox1[0]
    y1_top = bbox1[1]
    x1_right = bbox1[2]
    y1_bot = bbox1[3]

    x2_left = bbox2[0]
    y2_top = bbox2[1]
    x2_right = bbox2[2]
    y2_bot = bbox2[3]

    x_left = max(x1_left, x2_left)
    x_right = min(x1_right, x2_right)
    y_top = max(y1_top, y2_top)
    y_bot = min(y1_bot, y2_bot)

    inter = (x_right - x_left) * (y_bot - y_top)
    if x_right < x_left or y_bot < y_top:
        return 0.0
    area1 = (x1_right - x1_left) * (y1_bot - y1_top)
    area2 = (x2_right - x2_left) * (y2_bot - y2_top)
    union = area1 + area2 - inter

    IoU = inter / union
    return IoU

total_people = 0
incorrect = 0
withmask = 0
withoutmask = 0
list_objects = []
isRemove = []
for i in result[1]:
    temp = i
    temp = np.append(temp, 1)
    list_objects.append(temp)
    isRemove.append(0)

for i in result[2]:
    temp = i 
    temp = np.append(temp, 2)
    list_objects.append(temp)
    isRemove.append(0)

for i in result[3]:
    temp = i
    temp = np.append(temp, 3)
    list_objects.append(temp)
    isRemove.append(0)

for i in range(len(list_objects) - 1):
    for j in range(i + 1, len(list_objects)):
        bbox1 = [list_objects[i][0], list_objects[i][1], list_objects[i][2], list_objects[i][3]]
        bbox2 = [list_objects[j][0], list_objects[j][1], list_objects[j][2], list_objects[j][3]]
        if abs(IoU(bbox1, bbox2)) > 0.7:
            if list_objects[i][4] > list_objects[j][4]:
                isRemove[j] = 1
            else:
                isRemove[i] = 1
            # print("IoU", abs(IoU(bbox1, bbox2)))
        

        if list_objects[i][4] < 0.4:
            isRemove[i] = 1
        if list_objects[j][4] < 0.4:
            isRemove[j] = 1

selected_list = []
for i in range(len(list_objects)):
    if isRemove[i] == 0:
        selected_list.append(list_objects[i])

for i in selected_list:
    if i[5] == 1:
        incorrect += 1
    elif i[5] == 2:
        withmask += 1
    elif i[5] ==3:
        withoutmask += 1
    
total_people += incorrect + withmask + withoutmask

img = 'demo2.png'  # or img = mmcv.imread(img), which will only load it once

img = cv2.imread(img)
for i in selected_list:
    if i[5] == 1:
        color = (255, 0, 0)
        text  = "Mask weared incorrect"
    elif i[5] == 2:
        color = (0, 255, 0)
        text  = "With mask"
    elif i[5] == 3:
        color = (0, 0, 255)
        text = "Without mask"
    text += ": " + str(round(i[4], 2))
    x1 = i[0]
    y1 = i[1]
    x2 = i[2] - 1
    y2 = i[3] - 1

    x1 = round(x1)
    y1 = round(y1)
    x2 = round(x2)
    y2 = round(y2)

    img = cv2.rectangle(img, (x1, y1), (x2, y2), color, 3)
    img = cv2.putText(img, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
cv2.imwrite('New_result.jpg', img)