-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
159 lines (139 loc) · 6.07 KB
/
detect.py
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
import torch
import cv2
import numpy as np
import os
import wx
from threading import Thread
from PIL import Image, ExifTags
def copy_exif_data(source_path, target_path):
# Open the source image to extract EXIF data
source_image = Image.open(source_path)
# Extract the EXIF data from the source image
exif_data = source_image._getexif()
if exif_data:
exif_binary = source_image.info["exif"]
# Open the target image
target_image = Image.open(target_path)
# Apply the EXIF data to the target image
target_image.save(target_path, exif=exif_binary)
print("EXIF data copied from source to target image.")
else:
print("No EXIF data found in the source image.")
def save_image(output_path, img, source_path):
cv2.imwrite(output_path, img)
copy_exif_data(source_path, output_path)
def detect_licenseplates(images, output, self):
progress = wx.ProgressDialog("Censoring in progress", "please wait", maximum=100, parent=self, style=wx.PD_SMOOTH|wx.PD_AUTO_HIDE)
percent = 0
progress.Update(percent)
model_path = 'models\\licenseplates.pt'
model = torch.hub.load('ultralytics/yolov5:master', 'custom', path=model_path)
model.eval()
classes = model.names
try:
os.mkdir(output)
except FileExistsError as error:
None
percent_per_image = int(100 / len(images))
current_image = 0
for image in images:
current_image += 1
percent += percent_per_image
progress.Update(percent, "Image %s/%s" % (current_image, len(images)))
img = cv2.imread(image)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
h, w, c = img.shape
results = model(img_rgb, w if w > h else h)
detected_objects = results.pandas().xyxy[0]
img_with_blur = img.copy()
for _, obj in detected_objects.iterrows():
box = [int(obj.iloc[0]), int(obj.iloc[1]), int(obj.iloc[2]), int(obj.iloc[3])]
x1, y1, x2, y2 = box
roi = img[y1:y2, x1:x2]
blurred_roi = cv2.GaussianBlur(roi, (21, 21), 0)
img_with_blur[y1:y2, x1:x2] = blurred_roi
output_image_path = os.path.join(output, os.path.basename(image))
save_image(output_image_path, img_with_blur, image)
thread = Thread(target=save_image, args=(output_image_path, img_with_blur, image))
thread.start()
progress.Destroy()
def detect_faces(images, output, self):
progress = wx.ProgressDialog("Censoring in progress", "please wait", maximum=100, parent=self, style=wx.PD_SMOOTH|wx.PD_AUTO_HIDE)
percent = 0
progress.Update(percent)
model_path = 'models\\faces.pt'
model = torch.hub.load('ultralytics/yolov5:master', 'custom', path=model_path) # 'custom' model for loading from a .pt file
model.eval()
classes = model.names
try:
os.mkdir(output)
except FileExistsError as error:
None
percent_per_image = int(100 / len(images))
current_image = 0
for image in images:
current_image += 1
percent += percent_per_image
progress.Update(percent, "Image %s/%s" % (current_image, len(images)))
img = cv2.imread(image)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
h, w, c = img.shape
results = model(img_rgb, w if w > h else h)
detected_objects = results.pandas().xyxy[0]
img_with_blur = img.copy()
for _, obj in detected_objects.iterrows():
box = [int(obj.iloc[0]), int(obj.iloc[1]), int(obj.iloc[2]), int(obj.iloc[3])]
x1, y1, x2, y2 = box
roi = img[y1:y2, x1:x2]
blurred_roi = cv2.GaussianBlur(roi, (51, 51), 0)
img_with_blur[y1:y2, x1:x2] = blurred_roi
output_image_path = os.path.join(output, os.path.basename(image))
save_image(output_image_path, img_with_blur, image)
thread = Thread(target=save_image, args=(output_image_path, img_with_blur, image))
thread.start()
progress.Destroy()
def detect_both(images, output, self):
progress = wx.ProgressDialog("Censoring in progress", "", maximum=100, parent=self, style=wx.PD_SMOOTH|wx.PD_AUTO_HIDE)
percent = 0
progress.Update(percent)
model1_path = 'models\\faces.pt'
model2_path = 'models\\licenseplates.pt'
model1 = torch.hub.load('ultralytics/yolov5:master', 'custom', path=model1_path)
model2 = torch.hub.load('ultralytics/yolov5:master', 'custom', path=model2_path)
model1.eval()
model2.eval()
try:
os.mkdir(output)
except FileExistsError as error:
None
percent_per_image = int(100 / len(images))
current_image = 0
for image in images:
current_image += 1
percent += percent_per_image
progress.Update(percent, "Image %s/%s" % (current_image, len(images)))
img = cv2.imread(image)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
h, w, c = img.shape
results = model1(img_rgb, w if w > h else h)
results2 = model2(img_rgb, w if w > h else h)
detected_objects1 = results.pandas().xyxy[0]
detected_objects2 = results2.pandas().xyxy[0]
img_with_blur = img.copy()
for _, obj in detected_objects1.iterrows():
box = [int(obj.iloc[0]), int(obj.iloc[1]), int(obj.iloc[2]), int(obj.iloc[3])]
x1, y1, x2, y2 = box
roi = img[y1:y2, x1:x2]
blurred_roi = cv2.GaussianBlur(roi, (21, 21), 0)
img_with_blur[y1:y2, x1:x2] = blurred_roi
for _, obj in detected_objects2.iterrows():
box = [int(obj.iloc[0]), int(obj.iloc[1]), int(obj.iloc[2]), int(obj.iloc[3])]
x1, y1, x2, y2 = box
roi = img[y1:y2, x1:x2]
blurred_roi = cv2.GaussianBlur(roi, (51, 51), 0)
img_with_blur[y1:y2, x1:x2] = blurred_roi
output_image_path = os.path.join(output, os.path.basename(image))
save_image(output_image_path, img_with_blur, image)
thread = Thread(target=save_image, args=(output_image_path, img_with_blur, image))
thread.start()
progress.Destroy()