Lam-Hung commited on
Commit
cdfca96
1 Parent(s): eb6b4f6

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +36 -3
README.md CHANGED
@@ -1,3 +1,36 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+ -------------------------HOW TO USE---------------------
5
+ from transformers import AutoProcessor, AutoModelForObjectDetection
6
+ from PIL import Image
7
+ import torch
8
+ import matplotlib.pyplot as plt
9
+
10
+ # colors for visualization
11
+ COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
12
+ [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]
13
+
14
+ def plot_results(pil_img, scores, labels, boxes):
15
+ plt.figure(figsize=(16,10))
16
+ plt.imshow(pil_img)
17
+ ax = plt.gca()
18
+ colors = COLORS * 100
19
+ for score, label, (xmin, ymin, xmax, ymax),c in zip(scores.tolist(), labels.tolist(), boxes.tolist(), colors):
20
+ ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,
21
+ fill=False, color=c, linewidth=3))
22
+ text = f'{model.config.id2label[label]}: {score:0.2f}'
23
+ ax.text(xmin, ymin, text, fontsize=15,
24
+ bbox=dict(facecolor='yellow', alpha=0.5))
25
+ plt.axis('off')
26
+ plt.show()
27
+
28
+ processor = AutoProcessor.from_pretrained('Lam-Hung/orange_detetion_model')
29
+ model = AutoModelForObjectDetection.from_pretrained('Lam-Hung/orange_detetion_model').to('cuda')
30
+
31
+ image = Image.open('/content/C59_59.jpeg')
32
+ inputs = processor(image, return_tensors="pt").to('cuda')
33
+ with torch.no_grad():
34
+ outputs = model(**inputs)
35
+ results = processor.post_process_object_detection(outputs, threshold=0.3, target_sizes=[image.size[::-1]])[0]
36
+ plot_results(image, results['scores'], results['labels'], results['boxes'])