-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
197 lines (178 loc) · 8.23 KB
/
app.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from ultralytics import YOLO
from ultralytics.engine.results import Results
from deepface import DeepFace
from PIL import Image
# import gradio as gr
import shutil
import pandas
import cv2
import os
#print("keeri1")
#fps=[0]#list aaki so fn inte velil acces eyam . golabal variale akiyalm mathi arkm maybe . bt ath parichatyam ila#THAT WAS NOT NEEDED
def extract_frames(video_path, output_folder, interval=1):
# Open the video file
print("keeri")
video_capture = cv2.VideoCapture(video_path)
# Check if the video file was opened successfully
if not video_capture.isOpened():
print("Error: Couldn't open the video file.")
return
# Initialize variables
frame_count = 0
# Loop through the video frames
while True:
# Read the next frame
ret, frame = video_capture.read()
# If no frame is retrieved, break the loop
if not ret:
break
# Save the frame if the frame count is a multiple of the interval
if frame_count % interval == 0:
# Save the frame as an image
cv2.imwrite(os.path.join(output_folder, f"frame_{frame_count}.jpg"), frame)
# Increment frame count
frame_count += 1
# Release the video capture object
video_capture.release()
def get_image_paths(directory):
image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp'] # Add more extensions if needed
image_paths = []
# Walk through all files and directories in the given directory
for root, dirs, files in os.walk(directory):
for file in files:
# Check if the file has a valid image extension
if any(file.lower().endswith(ext) for ext in image_extensions):
image_paths.append(os.path.join(root, file))
return image_paths
def process(image_paths):
op=0
for i in image_paths:
#input_image="/content/output_folder/frame_129.jpg"
input_image=i
model = YOLO(r'.\best.pt')
results: Results = model.predict(input_image)[0]
#image = Image.open("/content/output_folder/frame_129.jpg")
image = Image.open(i)
detected_objects = []
if hasattr(results, 'boxes') and hasattr(results, 'names'):
for box in results.boxes.xyxy:
object_id = int(box[-1])
object_name = results.names.get(object_id)
x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])
detected_objects.append((object_name, (x1, y1, x2, y2)))
# Create or clear the 'faces' directory
print("faces ondakan pon")
if os.path.exists("faces"):
pass
#shutil.rmtree("faces")#NJAN CHYTHE COMMENT
else:
os.makedirs("faces")
extracted_names = []
# Crop and save each detected object
for i, (object_name, (x1, y1, x2, y2)) in enumerate(detected_objects):
object_image = image.crop((x1, y1, x2, y2))
object_image.save(f"faces/face{i}.jpg")
cropped_objects_dir = "./faces/"
for filename in os.listdir(cropped_objects_dir):
if filename.endswith(".jpg"):
print(f"file name is {filename}")
img_path = os.path.join(cropped_objects_dir, filename)
#img_path="/content/faces/face0.jpg"
model = DeepFace.find(img_path=img_path, db_path=r".\database", enforce_detection=False, model_name="Facenet512")
print("model is")
print(model)
#model[0]['identity']
'''if model and len(model[0]['identity']) > 0:
break'''
if model and len(model[0]['identity']) > 0:
op=1
# Extract the name and append it to the list
print("model is")
print(model[0]["identity"],len(model[0]["identity"]))
path=model[0]['identity'][0].split('\\')
print(path)
name = path[3]#play with the indexz bae
print(f"name is {name}")
# Save the known face into the 'known' folder
known_faces_dir=r".\known"
known_faces_path = os.path.join(known_faces_dir, f"{len(extracted_names) + 1}_{name}.jpg")
print(f"image path is {img_path}, and knownface path is {known_faces_path}")
shutil.copy(img_path, known_faces_path)
shutil.copy(input_image, known_faces_dir)
if op==1:
print("kitti mwoneeeee")
shutil.copy(input_image, known_faces_dir)
cv2.imshow('Video', i)
#frame edkane koode add akanmcode
return input_image
break
def clear_folder(folder_path):
# List all files in the folder
files = os.listdir(folder_path)
print(files)
# Iterate through each file and remove it
for file_name in files:
file_path = os.path.join(folder_path, file_name)
if os.path.isfile(file_path):
os.remove(file_path)
def live_process(image_path):
op=0
#input_image="/content/output_folder/frame_129.jpg"
input_image=image_path
model = YOLO(r'.\best.pt')
results: Results = model.predict(input_image)[0]
#image = Image.open("/content/output_folder/frame_129.jpg")
image = Image.open(i)
detected_objects = []
if hasattr(results, 'boxes') and hasattr(results, 'names'):
for box in results.boxes.xyxy:
object_id = int(box[-1])
object_name = results.names.get(object_id)
x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])
detected_objects.append((object_name, (x1, y1, x2, y2)))
# Create or clear the 'faces' directory
print("faces ondakan pon")
if os.path.exists("faces"):
pass
#shutil.rmtree("faces")#NJAN CHYTHE COMMENT
else:
os.makedirs("faces")
extracted_names = []
# Crop and save each detected object
for i, (object_name, (x1, y1, x2, y2)) in enumerate(detected_objects):
object_image = image.crop((x1, y1, x2, y2))
object_image.save(f"faces/face{i}.jpg")
cropped_objects_dir = "./faces/"
for filename in os.listdir(cropped_objects_dir):
if filename.endswith(".jpg"):
print(f"file name is {filename}")
img_path = os.path.join(cropped_objects_dir, filename)
#img_path="/content/faces/face0.jpg"
model = DeepFace.find(img_path=img_path, db_path=r".\database", enforce_detection=False, model_name="Facenet512")
print("model is")
print(model)
#model[0]['identity']
'''if model and len(model[0]['identity']) > 0:
break'''
if model and len(model[0]['identity']) > 0:
op=1
# Extract the name and append it to the list
print("model is")
print(model[0]["identity"],len(model[0]["identity"]))
path=model[0]['identity'][0].split('\\')
print(path)
name = path[3]#play with the indexz bae
print(f"name is {name}")
# Save the known face into the 'known' folder
known_faces_dir=r".\known"
known_faces_path = os.path.join(known_faces_dir, f"{len(extracted_names) + 1}_{name}.jpg")
print(f"image path is {img_path}, and knownface path is {known_faces_path}")
shutil.copy(img_path, known_faces_path)
shutil.copy(input_image, known_faces_dir)
if op==1:
print("kitti mwoneeeee")
return True
shutil.copy(input_image, known_faces_dir)
cv2.imshow('Video', i)
#frame edkane koode add akanmcode
return input_image