lms7127 commited on
Commit
4183e41
·
1 Parent(s): ccc218f

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +46 -46
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
- 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")
 
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")