Instructions to use lms7127/table_detr_30ep with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use lms7127/table_detr_30ep with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("object-detection", model="lms7127/table_detr_30ep")# pip install -U transformers accelerate # Load model directly from transformers import AutoImageProcessor, AutoModelForObjectDetection processor = AutoImageProcessor.from_pretrained("lms7127/table_detr_30ep") model = AutoModelForObjectDetection.from_pretrained("lms7127/table_detr_30ep", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -3,49 +3,49 @@
|
|
| 3 |
코드 예시
|
| 4 |
|
| 5 |
metrics:
|
| 6 |
-
|
| 7 |
-
import os
|
| 8 |
-
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 9 |
-
import torch
|
| 10 |
-
import cv2
|
| 11 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 12 |
-
|
| 13 |
-
model = DetrForObjectDetection.from_pretrained("lms7127/table_detr_30ep")
|
| 14 |
-
processor = DetrImageProcessor.from_pretrained("lms7127/table_detr_30ep")
|
| 15 |
-
|
| 16 |
-
font_path = os.path.join(cv2.__path__[0],'qt','fonts','DejaVuSans.ttf')
|
| 17 |
-
|
| 18 |
-
#변환할 이미지 목록 불러오기
|
| 19 |
-
image_path = '/path/to/image'
|
| 20 |
-
save_path ="/path/to/save"
|
| 21 |
-
|
| 22 |
-
img = Image.open(image_path)
|
| 23 |
-
inputs = processor(images=img, return_tensors="pt")
|
| 24 |
-
with torch.no_grad():#추가학습 방지
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# convert outputs (bounding boxes and class logits) to COCO API
|
| 28 |
-
# let's only keep detections with score > 0.9
|
| 29 |
-
width, height = img.size
|
| 30 |
-
postprocessed_outputs = processor.post_process_object_detection(outputs,
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
results = postprocessed_outputs[0]
|
| 34 |
-
draw = ImageDraw.Draw(img)
|
| 35 |
-
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
img.save(save_path,"JPEG")
|
|
|
|
| 3 |
코드 예시
|
| 4 |
|
| 5 |
metrics:
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 9 |
+
import torch
|
| 10 |
+
import cv2
|
| 11 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 12 |
+
|
| 13 |
+
model = DetrForObjectDetection.from_pretrained("lms7127/table_detr_30ep")
|
| 14 |
+
processor = DetrImageProcessor.from_pretrained("lms7127/table_detr_30ep")
|
| 15 |
+
|
| 16 |
+
font_path = os.path.join(cv2.__path__[0],'qt','fonts','DejaVuSans.ttf')
|
| 17 |
+
|
| 18 |
+
#변환할 이미지 목록 불러오기
|
| 19 |
+
image_path = '/path/to/image'
|
| 20 |
+
save_path ="/path/to/save"
|
| 21 |
+
|
| 22 |
+
img = Image.open(image_path)
|
| 23 |
+
inputs = processor(images=img, return_tensors="pt")
|
| 24 |
+
with torch.no_grad():#추가학습 방지
|
| 25 |
+
outputs = model(**inputs)
|
| 26 |
+
|
| 27 |
+
# convert outputs (bounding boxes and class logits) to COCO API
|
| 28 |
+
# let's only keep detections with score > 0.9
|
| 29 |
+
width, height = img.size
|
| 30 |
+
postprocessed_outputs = processor.post_process_object_detection(outputs,
|
| 31 |
+
target_sizes=[(height, width)],
|
| 32 |
+
threshold=0.7)
|
| 33 |
+
results = postprocessed_outputs[0]
|
| 34 |
+
draw = ImageDraw.Draw(img)
|
| 35 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 36 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 37 |
+
class_label = model.config.id2label[label.item()]
|
| 38 |
+
confidence = round(score.item(), 3)
|
| 39 |
+
|
| 40 |
+
# Draw rectangle
|
| 41 |
+
draw.rectangle(box, outline="red", width = 5)
|
| 42 |
+
|
| 43 |
+
# Add text
|
| 44 |
+
font_size=50
|
| 45 |
+
font = ImageFont.truetype(font_path, font_size)
|
| 46 |
+
text = f"{class_label}: {confidence}"
|
| 47 |
+
text_width, text_height = draw.textsize(text)
|
| 48 |
+
text_location = [box[0], box[1] - text_height - 4]
|
| 49 |
+
draw.rectangle([text_location[0], text_location[1], text_location[0] + text_width, text_location[1] + text_height], fill="red")
|
| 50 |
+
draw.text(text_location, text, fill="white", font=font)
|
| 51 |
+
img.save(save_path,"JPEG")
|