Spaces:
Running
Running
Synced repo using 'sync_with_huggingface' Github Action
Browse files- main.py +13 -8
- templates/index.html +19 -21
main.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
# Import Fast API
|
2 |
from fastapi import FastAPI, Request, UploadFile, File
|
3 |
from fastapi.templating import Jinja2Templates
|
4 |
-
|
5 |
|
6 |
# Import bytes
|
7 |
from io import BytesIO
|
@@ -44,14 +44,14 @@ model = YOLO(model_path)
|
|
44 |
# Index route
|
45 |
@app.get("/")
|
46 |
async def root(request: Request):
|
47 |
-
context = {"request": request}
|
48 |
# Render index.html
|
49 |
-
return templates.TemplateResponse("index.html",
|
50 |
|
51 |
|
52 |
# Upload images decorator
|
53 |
@app.post("/predict-img")
|
54 |
-
def predict_image(file: UploadFile = File(...)):
|
|
|
55 |
try:
|
56 |
# Try to read the file
|
57 |
contents = file.file.read()
|
@@ -88,8 +88,13 @@ def predict_image(file: UploadFile = File(...)):
|
|
88 |
results.save(img_bytes, "JPEG")
|
89 |
img_bytes.seek(0)
|
90 |
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
93 |
|
94 |
-
|
95 |
-
|
|
|
|
1 |
# Import Fast API
|
2 |
from fastapi import FastAPI, Request, UploadFile, File
|
3 |
from fastapi.templating import Jinja2Templates
|
4 |
+
import base64
|
5 |
|
6 |
# Import bytes
|
7 |
from io import BytesIO
|
|
|
44 |
# Index route
|
45 |
@app.get("/")
|
46 |
async def root(request: Request):
|
|
|
47 |
# Render index.html
|
48 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
49 |
|
50 |
|
51 |
# Upload images decorator
|
52 |
@app.post("/predict-img")
|
53 |
+
def predict_image(request: Request, file: UploadFile = File(...)):
|
54 |
+
|
55 |
try:
|
56 |
# Try to read the file
|
57 |
contents = file.file.read()
|
|
|
88 |
results.save(img_bytes, "JPEG")
|
89 |
img_bytes.seek(0)
|
90 |
|
91 |
+
img_bytes = base64.b64encode(img_bytes.getvalue()).decode()
|
92 |
+
|
93 |
+
try:
|
94 |
+
os.remove(image)
|
95 |
+
except Exception as e:
|
96 |
+
logging.error(f"Error deleting image: {e}")
|
97 |
|
98 |
+
return templates.TemplateResponse(
|
99 |
+
"index.html", {"request": request, "img": img_bytes}
|
100 |
+
)
|
templates/index.html
CHANGED
@@ -6,7 +6,7 @@
|
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
7 |
<meta
|
8 |
name="viewport"
|
9 |
-
content="width=device-width, initial-scale=1, shrink-to-fit=
|
10 |
/>
|
11 |
|
12 |
<!-- Bootstrap CSS -->
|
@@ -23,30 +23,28 @@
|
|
23 |
crossorigin="anonymous"
|
24 |
></script>
|
25 |
|
26 |
-
<title>
|
27 |
</head>
|
28 |
<body>
|
29 |
-
<div class="
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
Upload files
|
41 |
</button>
|
42 |
-
</
|
43 |
-
</form>
|
44 |
-
<!-- Optional JavaScript; choose one of the two! -->
|
45 |
-
|
46 |
-
<!-- Option 1: Bootstrap Bundle with Popper -->
|
47 |
-
|
48 |
-
<!-- Option 2: Separate Popper and Bootstrap JS -->
|
49 |
|
|
|
|
|
|
|
|
|
|
|
50 |
<script
|
51 |
src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"
|
52 |
integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
|
|
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
7 |
<meta
|
8 |
name="viewport"
|
9 |
+
content="width=device-width, initial-scale=1, shrink-to-fit=yes"
|
10 |
/>
|
11 |
|
12 |
<!-- Bootstrap CSS -->
|
|
|
23 |
crossorigin="anonymous"
|
24 |
></script>
|
25 |
|
26 |
+
<title>Face detector</title>
|
27 |
</head>
|
28 |
<body>
|
29 |
+
<div class="text-center p-5">
|
30 |
+
<h2 class="text-center p-2">Face detection App</h2>
|
31 |
+
<label for="formFile" class="form-label"
|
32 |
+
>Upload file to detect faces on the image</label
|
33 |
+
>
|
34 |
+
<form method="post" action="/predict-img" enctype="multipart/form-data">
|
35 |
+
<div class="w-25 p-2 mx-auto">
|
36 |
+
<input class="form-control" type="file" id="formFile" name="file" />
|
37 |
+
</div>
|
38 |
+
<button type="submit" class="btn btn-primary mx-auto ml-4">
|
39 |
+
Detect faces
|
|
|
40 |
</button>
|
41 |
+
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
{% if img %}
|
44 |
+
<h2 class="text-center p-4">Predicted faces</h2>
|
45 |
+
<img src="data:image/jpeg;base64,{{ img }}" />
|
46 |
+
{% endif %}
|
47 |
+
</div>
|
48 |
<script
|
49 |
src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"
|
50 |
integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
|