smarttiger commited on
Commit
4ed0e52
1 Parent(s): 8ffef55

本地测试facebook/detr-resnet-50 的 object detected 库,使用本地缓存测试。

Browse files
.idea/ipcamera.iml CHANGED
@@ -1,8 +1,10 @@
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <module type="PYTHON_MODULE" version="4">
3
  <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$" />
5
- <orderEntry type="inheritedJdk" />
 
 
6
  <orderEntry type="sourceFolder" forTests="false" />
7
  </component>
8
  </module>
 
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <module type="PYTHON_MODULE" version="4">
3
  <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/venv" />
6
+ </content>
7
+ <orderEntry type="jdk" jdkName="Python 3.9 (ipcamera)" jdkType="Python SDK" />
8
  <orderEntry type="sourceFolder" forTests="false" />
9
  </component>
10
  </module>
.idea/misc.xml CHANGED
@@ -1,4 +1,4 @@
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
- <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (chatGptApi)" project-jdk-type="Python SDK" />
4
  </project>
 
1
  <?xml version="1.0" encoding="UTF-8"?>
2
  <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (ipcamera)" project-jdk-type="Python SDK" />
4
  </project>
models/facebook/detr-resnet-50.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import DetrImageProcessor, DetrForObjectDetection
2
+ import torch
3
+ from PIL import Image
4
+ import requests
5
+
6
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
7
+ image = Image.open(requests.get(url, stream=True).raw)
8
+
9
+ # you can specify the revision tag if you don't want the timm dependency
10
+ processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm", local_files_only=True)
11
+ model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm", local_files_only=True)
12
+
13
+ inputs = processor(images=image, return_tensors="pt")
14
+
15
+ outputs = model(**inputs)
16
+
17
+ # convert outputs (bounding boxes and class logits) to COCO API
18
+ # let's only keep detections with score > 0.9
19
+ target_sizes = torch.tensor([image.size[::-1]])
20
+ results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
21
+
22
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
23
+ box = [round(i, 2) for i in box.tolist()]
24
+ print(
25
+ f"Detected {model.config.id2label[label.item()]} with confidence "
26
+ f"{round(score.item(), 3)} at location {box}"
27
+ )