-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_linux.py
145 lines (118 loc) · 5.5 KB
/
main_linux.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
from fastapi import FastAPI, File, UploadFile, Request, WebSocket, Response
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from PIL import Image
import io
import os
import uvicorn # Import Uvicorn
import cv2
import face_recognition
import os
import time
import sys
from pathlib import Path
import webbrowser
import zipfile
import shutil
from datetime import timedelta
app = FastAPI()
templates = Jinja2Templates(directory="templates")
# Create a folder to store the uploaded and processed images
os.makedirs ("images", exist_ok=True)
os.makedirs ("images_tmp", exist_ok=True)
# Mount the folder as a static file directory
app.mount ("/images", StaticFiles (directory="images"), name="images")
app.mount ("/images_tmp", StaticFiles (directory="images_tmp"), name="images_tmp")
def deletetmp():
folder = 'images'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
def deletetmp2(folder):
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
app.mount("/static", StaticFiles(directory="static"), name="static")
# Define a route to display the web UI
@app.get ("/", response_class=HTMLResponse)
async def index(request:Request):
# Return a simple HTML form to upload a file
return templates.TemplateResponse("index.html", {"request": request, "message": None})
# Define a route to handle the file upload
@app.post ("/uploadfile/")
async def create_upload_file(img: UploadFile = File (...), files: list[UploadFile] = File (...)):
# Save the uploaded file to the images folder
file_path = os.path.join ("images", img.filename)
image_path = file_path
print(file_path)
for file in files:
# Save the uploaded file to the images folder
pathdir = os.path.join ("images_tmp", file.filename)
print(pathdir)
substring=pathdir.split("/")
os.makedirs ("images_tmp/"+substring[1], exist_ok=True)
folder_files = os.path.join ("images_tmp", file.filename)
#os.makedirs (folder_files, exist_ok=True)
with open (folder_files, "wb") as f:
f.write (file.file.read())
with open (file_path, "wb") as f:
f.write (img.file.read ())
pictures_folder = os.path.join ("images_tmp/"+substring[1])
total_files = len([name for name in os.listdir(pictures_folder) if os.path.isfile(os.path.join(pictures_folder, name))])
processed_files = 0
docs_img = cv2.imread(f"{image_path}")
docs_rgb_img = cv2.cvtColor(docs_img, cv2.COLOR_BGR2RGB)
docs_img_encoding = face_recognition.face_encodings(docs_rgb_img)[0]
start_time = time.time()
os.makedirs ("images_tmp/"+substring[1]+"_FaceDetected", exist_ok=True)
foldername = os.path.join(os.getcwd(), "images_tmp/"+substring[1]+"_FaceDetected")
#os.startfile(folder_path2)
os.system('xdg-open "%s"' % foldername)
for root, dirs, files in os.walk(pictures_folder):
for file in files:
if file.endswith(".jpeg") or file.endswith(".jpg") or file.endswith(".png"):
img_path = os.path.join(root, file)
img = cv2.imread(img_path)
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#img_encoding = face_recognition.face_encodings(rgb_img)[0]
encodings = face_recognition.face_encodings(rgb_img)
if len(encodings) > 0:
img_encoding = encodings[0]
# do something with img_encoding
else:
print("No faces found in the image")
result = face_recognition.compare_faces([docs_img_encoding], img_encoding)
processed_files += 1
elapsed_time = time.time() - start_time
estimated_time = (elapsed_time / processed_files) * (total_files - processed_files)
estimated_time_delta = timedelta(seconds=int(estimated_time))
print(f"Processed {processed_files} out of {total_files} files. Estimated time left: {estimated_time_delta} seconds.")
if result[0]:
cv2.imshow(" ",img)
#cv2.waitKey(0)
cv2.destroyAllWindows()
Face_folder = os.path.join ("images_tmp/"+substring[1]+"_FaceDetected")
print(f"File name: {file}, Location: {img_path}")
shutil.copy(img_path, Face_folder)
if input() == 'q':
break
deletetmp()
deletetmp2(pictures_folder)
#return templates.TemplateResponse("gallery.html", {"request":None , "image_files": pictures_folder})
# Add a conditional statement to run Uvicorn if the file is the main script
if __name__ == "__main__":
webbrowser.open("http://localhost:8000/")
uvicorn.run("main_linux:app", host="0.0.0.0", port=8000, reload=True)