Upload stat_lab_10.py
Browse files- stat_lab_10.py +63 -0
stat_lab_10.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""stat_lab_10.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1M9jt20Xv08CFH0RJOpWe8aXT62PqGrKu
|
8 |
+
"""
|
9 |
+
|
10 |
+
!python -m pip install transformers accelerate sentencepiece emoji pythainlp --quiet
|
11 |
+
!python -m pip install --no-deps thai2transformers==0.1.2 --quiet
|
12 |
+
|
13 |
+
"""# image Detection"""
|
14 |
+
|
15 |
+
!pip install timm
|
16 |
+
|
17 |
+
"""## pipline"""
|
18 |
+
|
19 |
+
# Use a pipeline as a high-level helper
|
20 |
+
from transformers import pipeline
|
21 |
+
|
22 |
+
pipe = pipeline("object-detection", model="facebook/detr-resnet-50")
|
23 |
+
|
24 |
+
"""## Load model"""
|
25 |
+
|
26 |
+
# Load model directly
|
27 |
+
from transformers import AutoFeatureExtractor, AutoModelForObjectDetection
|
28 |
+
|
29 |
+
extractor = AutoFeatureExtractor.from_pretrained("facebook/detr-resnet-50")
|
30 |
+
model = AutoModelForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
31 |
+
|
32 |
+
"""## Use model"""
|
33 |
+
|
34 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
35 |
+
import torch
|
36 |
+
from PIL import Image
|
37 |
+
import requests
|
38 |
+
|
39 |
+
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
40 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
41 |
+
|
42 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
43 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
44 |
+
|
45 |
+
inputs = processor(images=image, return_tensors="pt")
|
46 |
+
outputs = model(**inputs)
|
47 |
+
|
48 |
+
# convert outputs (bounding boxes and class logits) to COCO API
|
49 |
+
# let's only keep detections with score > 0.9
|
50 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
51 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
52 |
+
|
53 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
54 |
+
box = [round(i, 2) for i in box.tolist()]
|
55 |
+
print(
|
56 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
57 |
+
f"{round(score.item(), 3)} at location {box}"
|
58 |
+
)
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
|