eoeooe commited on
Commit
279c6b7
·
verified ·
1 Parent(s): 78e386f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -53
app.py CHANGED
@@ -1,54 +1,29 @@
1
  import gradio as gr
2
- from transformers import DetrImageProcessor, DetrForObjectDetection
3
- from PIL import Image, ImageDraw
4
- import torch
5
- import io
6
-
7
- # โหลดโมเดล Hugging Face DETR
8
- processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
9
- model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
10
-
11
- def detect_and_crop(image: Image.Image):
12
- # เตรียม input
13
- inputs = processor(images=image, return_tensors="pt")
14
- with torch.no_grad():
15
- outputs = model(**inputs)
16
-
17
- # postprocess
18
- target_sizes = torch.tensor([image.size[::-1]]) # (height, width)
19
- results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
20
-
21
- if len(results["boxes"]) == 0:
22
- return image, []
23
-
24
- # วารอบบนภาพต้นฉบับ
25
- image_with_boxes = image.copy()
26
- draw = ImageDraw.Draw(image_with_boxes)
27
-
28
- cropped_images = []
29
-
30
- for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
31
- box = [round(i, 2) for i in box.tolist()]
32
- label_name = model.config.id2label[label.item()]
33
- draw.rectangle(box, outline="red", width=3)
34
- draw.text((box[0], box[1] - 10), f"{label_name} ({score:.2f})", fill="red")
35
-
36
- # Crop และเก็บใน list
37
- cropped = image.crop(box)
38
- cropped_images.append(cropped)
39
-
40
- return image_with_boxes, cropped_images
41
-
42
- # UI ด้วย Gradio
43
- interface = gr.Interface(
44
- fn=detect_and_crop,
45
- inputs=gr.Image(type="pil", label="อัปโหลดภาพ"),
46
- outputs=[
47
- gr.Image(type="pil", label="ภาพที่มีกรอบวัตถุ"),
48
- gr.Gallery(label="Crop วัตถุที่เจอ").style(grid=3, height="auto")
49
- ],
50
- title="🔍 ตรวจจับวัตถุด้วย DETR (Hugging Face) และ Crop",
51
- description="อัปโหลดภาพ แล้วระบบจะตรวจจับวัตถุ (threshold > 90%) และแสดงภาพที่ crop แล้ว"
52
- )
53
-
54
- interface.launch()
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
4
+
5
+ # โหลด model และ processor จาก Hugging Face
6
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
7
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
8
+
9
+ def ocr_on_cropped(image: Image.Image):
10
+ if image is None:
11
+ return "กรุณาอัปโหลดและครอบภาพก่อน"
12
+
13
+ # แปลงภาพเป็น input ของ HuggingFace model
14
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
15
+ generated_ids = model.generate(pixel_values)
16
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
17
+
18
+ return generated_text.strip()
19
+
20
+ with gr.Blocks() as demo:
21
+ gr.Markdown("## 🤗 OCR ด้วย HuggingFace - อัปโหลดและครอบภาพ")
22
+
23
+ with gr.Row():
24
+ image_input = gr.Image(type="pil", interactive=True, label="อัปโหลและครอบภาพ")
25
+ ocr_result = gr.Textbox(label="ข้อความที่ตรวจพบ")
26
+
27
+ image_input.change(fn=ocr_on_cropped, inputs=image_input, outputs=ocr_result)
28
+
29
+ demo.launch()