Zeldeo commited on
Commit
9f0ff72
·
verified ·
1 Parent(s): ab29d14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -42
app.py CHANGED
@@ -1,20 +1,11 @@
1
  from fastapi import FastAPI, File, UploadFile
2
  from fastapi.responses import JSONResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
- import requests
5
  import io
 
 
6
 
7
- # -----------------------------
8
- # CONFIG
9
- # -----------------------------
10
- HF_API_URL = "https://api-inference.huggingface.co/models/czczup/textnet-base"
11
- HF_TOKEN = "YOUR_HF_TOKEN" # ⚠️ Mets ton token ici
12
-
13
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
14
-
15
- # -----------------------------
16
- # FASTAPI SETUP
17
- # -----------------------------
18
  app = FastAPI()
19
 
20
  app.add_middleware(
@@ -24,39 +15,30 @@ app.add_middleware(
24
  allow_headers=["*"],
25
  )
26
 
27
- # -----------------------------
28
- # ROUTE /detect
29
- # -----------------------------
 
30
  @app.post("/detect")
31
  async def detect_text(file: UploadFile = File(...)):
32
  try:
33
- # Lire l'image envoyée
34
  image_bytes = await file.read()
 
35
 
36
- # Appel à l'Inference API
37
- response = requests.post(
38
- HF_API_URL,
39
- headers=headers,
40
- data=image_bytes
41
- )
42
 
43
- if response.status_code != 200:
44
- return JSONResponse(
45
- {"success": False, "error": response.text},
46
- status_code=500
47
- )
48
 
49
- detections = response.json()
 
 
 
 
50
 
51
- # Format attendu par ton Flutter
52
  boxes = []
53
- for det in detections:
54
- poly = det.get("polygon", [])
55
- score = det.get("score", 0)
56
-
57
- if not poly:
58
- continue
59
-
60
  xs = [p[0] for p in poly]
61
  ys = [p[1] for p in poly]
62
 
@@ -70,12 +52,10 @@ async def detect_text(file: UploadFile = File(...)):
70
  })
71
 
72
  return JSONResponse({
73
- "boxes": boxes,
74
- "success": True
 
75
  })
76
 
77
  except Exception as e:
78
- return JSONResponse(
79
- {"success": False, "error": str(e)},
80
- status_code=500
81
- )
 
1
  from fastapi import FastAPI, File, UploadFile
2
  from fastapi.responses import JSONResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
+ from PIL import Image
5
  import io
6
+ import torch
7
+ from transformers import AutoImageProcessor, AutoModelForObjectDetection
8
 
 
 
 
 
 
 
 
 
 
 
 
9
  app = FastAPI()
10
 
11
  app.add_middleware(
 
15
  allow_headers=["*"],
16
  )
17
 
18
+ processor = AutoImageProcessor.from_pretrained("czczup/textnet-base")
19
+ model = AutoModelForObjectDetection.from_pretrained("czczup/textnet-base")
20
+ model.eval()
21
+
22
  @app.post("/detect")
23
  async def detect_text(file: UploadFile = File(...)):
24
  try:
 
25
  image_bytes = await file.read()
26
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
27
 
28
+ inputs = processor(images=image, return_tensors="pt")
 
 
 
 
 
29
 
30
+ with torch.no_grad():
31
+ outputs = model(**inputs)
 
 
 
32
 
33
+ results = processor.post_process_object_detection(
34
+ outputs,
35
+ threshold=0.3,
36
+ target_sizes=[image.size[::-1]]
37
+ )[0]
38
 
 
39
  boxes = []
40
+ for poly, score in zip(results["polygons"], results["scores"]):
41
+ poly = poly.tolist()
 
 
 
 
 
42
  xs = [p[0] for p in poly]
43
  ys = [p[1] for p in poly]
44
 
 
52
  })
53
 
54
  return JSONResponse({
55
+ "image_width": image.width,
56
+ "image_height": image.height,
57
+ "boxes": boxes
58
  })
59
 
60
  except Exception as e:
61
+ return JSONResponse({"success": False, "error": str(e)}, status_code=500)