当前位置:网站首页>基于PyTorch和Fast RCNN快速实现目标识别
基于PyTorch和Fast RCNN快速实现目标识别
2022-07-06 06:35:00 【GIS开发者】
Faster RCNN,相对于R-CNN在结构上,Faster RCNN已经将特征抽取(feature extraction),proposal提取,bounding box regression(rect refine),classification都整合在了一个网络中,使得综合性能有较大提高,在检测速度方面尤为明显。
PyTorch是一个开源的Python机器学习库,基于Torch,用于自然语言处理等应用程序。相对于TensorFlow更加轻量化,更适合于科研和小型项目的使用。
这里介绍一个简单的例子,基于Fast RCNN算法和PyTorch快速实现目标识别。这里使用的是coco数据集已经训练好的在线的模型,运行起来比较简单。
代码
from PIL import Image
import matplotlib.pyplot as plt
# pip install -U matplotlib
import torch
# pip install pytorch
import torchvision.transforms as T
import torchvision
# pip install torchvision
import numpy as np
import cv2
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
# pip install opencv-python
# 下载已经训练好的模型
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
COCO_INSTANCE_CATEGORY_NAMES = [
'__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',
'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]
def get_prediction(img_path, threshold):
img = Image.open(img_path)
# 转换一个PIL库的图片或者numpy的数组为tensor张量类型;转换从[0,255]->[0,1]
transform = T.Compose([T.ToTensor()])
img = transform(img)
pred = model([img])
print(pred[0]['labels'].numpy())
# 类别提取
pred_class = [COCO_INSTANCE_CATEGORY_NAMES[i] for i in list(pred[0]['labels'].numpy())]
# 坐标提取
pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().numpy())]
# 找出符合相似度要求的
pred_score = list(pred[0]['scores'].detach().numpy())
pred_t = [pred_score.index(x) for x in pred_score if x > threshold][-1]
pred_boxes = pred_boxes[:pred_t + 1]
pred_class = pred_class[:pred_t + 1]
print("pred_class:", pred_class)
print("pred_boxes:", pred_boxes)
return pred_boxes, pred_class
def object_detection_api(img_path, threshold=0.5, rect_th=3, text_size=3, text_th=3):
boxes, pred_cls = get_prediction(img_path, threshold)
img = cv2.imread(img_path)
# 转换为RGB图像
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for i in range(len(boxes)):
# 根据坐标圈出目标
cv2.rectangle(img, (int(boxes[i][0][0]), int(boxes[i][0][1])), (int(boxes[i][1][0]), int(boxes[i][1][1])),
color=(0, 255, 0),
thickness=rect_th)
# 标注类别
cv2.putText(img, pred_cls[i], (int(boxes[i][0][0]), int(boxes[i][0][1])), cv2.FONT_HERSHEY_SIMPLEX, text_size,
(0, 255, 0), thickness=text_th)
plt.imshow(img)
plt.show()
if __name__ == '__main__':
object_detection_api(img_path=r"C:\Users\hanbo\Pictures\dog.jpg")
结果
示例1
示例2

边栏推荐
- Machine learning plant leaf recognition
- 翻译生物医学说明书,英译中怎样效果佳
- SAP SD发货流程中托盘的管理
- Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
- Reflex WMS medium level series 3: display shipped replaceable groups
- 金融德语翻译,北京专业的翻译公司
- MySQL high frequency interview 20 questions, necessary (important)
- 利用快捷方式-LNK-上线CS
- Office doc add in - Online CS
- 成功解决TypeError: data type ‘category‘ not understood
猜你喜欢

Fedora/REHL 安装 semanage

CS certificate fingerprint modification

LeetCode 732. My schedule III

万丈高楼平地起,每个API皆根基
![[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)](/img/3c/c25e7cbef9be1860842e8981f72352.png)
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)

Phishing & filename inversion & Office remote template

Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple

云服务器 AccessKey 密钥泄露利用

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Reflex WMS medium level series 3: display shipped replaceable groups
随机推荐
翻译公司证件盖章的价格是多少
How to translate professional papers and write English abstracts better
机器学习植物叶片识别
翻译影视剧字幕,这些特点务必要了解
Luogu p2089 roast chicken
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
基于购买行为数据对超市顾客进行市场细分(RFM模型)
University of Manchester | dda3c: collaborative distributed deep reinforcement learning in swarm agent systems
商标翻译有什么特点,如何翻译?
关于新冠疫情,常用的英文单词、语句有哪些?
Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
雲上有AI,讓地球科學研究更省力
Data security -- 13 -- data security lifecycle management
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
Fedora/rehl installation semanage
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
专业论文翻译,英文摘要如何写比较好
Number of query fields
[Yu Yue education] Dunhuang Literature and art reference materials of Zhejiang Normal University
Modify the list page on the basis of jeecg boot code generation (combined with customized components)