Spaces:
Sleeping
Sleeping
Raja Yeruva commited on
Commit ·
2c91c5c
1
Parent(s): db3afc5
Fix
Browse files- app/main.py +25 -59
app/main.py
CHANGED
|
@@ -67,70 +67,36 @@ async def predict_similarity(request: Request, file1: UploadFile = File(...), fi
|
|
| 67 |
global simi_filename1
|
| 68 |
global simi_filename2
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
contents = await file1.read()
|
| 72 |
-
simi_filename1 = 'app/static/' + file1.filename
|
| 73 |
-
with open(simi_filename1, 'wb') as f:
|
| 74 |
-
f.write(contents)
|
| 75 |
-
|
| 76 |
-
# Save second image
|
| 77 |
-
contents = await file2.read()
|
| 78 |
-
simi_filename2 = 'app/static/' + file2.filename
|
| 79 |
-
with open(simi_filename2, 'wb') as f:
|
| 80 |
-
f.write(contents)
|
| 81 |
-
|
| 82 |
-
# img1 = Image.open(simi_filename1)
|
| 83 |
-
# img1 = np.array(img1).reshape(img1.size[1], img1.size[0], 3).astype(np.uint8)
|
| 84 |
-
|
| 85 |
-
# img2 = Image.open(simi_filename2)
|
| 86 |
-
# img2 = np.array(img2).reshape(img2.size[1], img2.size[0], 3).astype(np.uint8)
|
| 87 |
-
|
| 88 |
-
img1 = Image.open(simi_filename1)
|
| 89 |
-
img1 = img1.convert('RGB') # Convert to RGB (ensures 3 channels)
|
| 90 |
-
img1 = np.array(img1).astype(np.uint8)
|
| 91 |
-
|
| 92 |
-
img2 = Image.open(simi_filename2)
|
| 93 |
-
img2 = img2.convert('RGB') # Convert to RGB (ensures 3 channels)
|
| 94 |
-
img2 = np.array(img2).astype(np.uint8)
|
| 95 |
-
|
| 96 |
-
result = face_recognition.get_similarity(img1, img2)
|
| 97 |
-
#print(result)
|
| 98 |
-
|
| 99 |
-
# Save first image
|
| 100 |
-
contents = await file1.read()
|
| 101 |
-
simi_filename1 = 'app/static/' + file1.filename
|
| 102 |
-
with open(simi_filename1, 'wb') as f:
|
| 103 |
-
f.write(contents)
|
| 104 |
-
|
| 105 |
-
# Save second image
|
| 106 |
-
contents = await file2.read()
|
| 107 |
-
simi_filename2 = 'app/static/' + file2.filename
|
| 108 |
-
with open(simi_filename2, 'wb') as f:
|
| 109 |
-
f.write(contents)
|
| 110 |
-
|
| 111 |
-
# With:
|
| 112 |
-
# Save first image (convert to PNG for web display)
|
| 113 |
-
contents = await file1.read()
|
| 114 |
import io
|
| 115 |
-
|
| 116 |
-
temp_img1 =
|
| 117 |
-
|
|
|
|
| 118 |
simi_filename1 = 'app/static/' + filename1
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
-
temp_img2 = Image.open(io.BytesIO(
|
| 124 |
-
|
| 125 |
-
filename2 = file2.filename.rsplit('.', 1)[0] + '.png'
|
| 126 |
simi_filename2 = 'app/static/' + filename2
|
| 127 |
-
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
#################################### Face Recognition endpoints #################################################
|
| 136 |
@app.get("/face_recognition/", name="face_recognition_root")
|
|
|
|
| 67 |
global simi_filename1
|
| 68 |
global simi_filename2
|
| 69 |
|
| 70 |
+
# Read and save first image as PNG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
import io
|
| 72 |
+
contents1 = await file1.read()
|
| 73 |
+
temp_img1 = Image.open(io.BytesIO(contents1))
|
| 74 |
+
temp_img1_rgb = temp_img1.convert('RGB')
|
| 75 |
+
filename1 = file1.filename.rsplit('.', 1)[0] + '.png'
|
| 76 |
simi_filename1 = 'app/static/' + filename1
|
| 77 |
+
temp_img1_rgb.save(simi_filename1, 'PNG')
|
| 78 |
+
|
| 79 |
+
# Read and save second image as PNG
|
| 80 |
+
contents2 = await file2.read()
|
| 81 |
+
temp_img2 = Image.open(io.BytesIO(contents2))
|
| 82 |
+
temp_img2_rgb = temp_img2.convert('RGB')
|
| 83 |
+
filename2 = file2.filename.rsplit('.', 1)[0] + '.png'
|
| 84 |
simi_filename2 = 'app/static/' + filename2
|
| 85 |
+
temp_img2_rgb.save(simi_filename2, 'PNG')
|
| 86 |
|
| 87 |
+
# Convert to numpy arrays for face recognition
|
| 88 |
+
img1 = np.array(temp_img1_rgb).astype(np.uint8)
|
| 89 |
+
img2 = np.array(temp_img2_rgb).astype(np.uint8)
|
| 90 |
+
|
| 91 |
+
# Get similarity
|
| 92 |
+
result = face_recognition.get_similarity(img1, img2)
|
| 93 |
|
| 94 |
+
return templates.TemplateResponse("predict_similarity.html", {
|
| 95 |
+
"request": request,
|
| 96 |
+
"result": np.round(result, 3),
|
| 97 |
+
"simi_filename1": request.url_for('static', path=filename1),
|
| 98 |
+
"simi_filename2": request.url_for('static', path=filename2),
|
| 99 |
+
})
|
| 100 |
|
| 101 |
#################################### Face Recognition endpoints #################################################
|
| 102 |
@app.get("/face_recognition/", name="face_recognition_root")
|