当前位置:网站首页>Fast target recognition based on pytorch and fast RCNN
Fast target recognition based on pytorch and fast RCNN
2022-07-06 06:53:00 【GIS developer】
Faster RCNN, be relative to R-CNN On the structure ,Faster RCNN Feature extraction has been done (feature extraction),proposal extract ,bounding box regression(rect refine),classification It's all integrated into one network , So that the comprehensive performance has been greatly improved , Especially in terms of detection speed .
PyTorch It's an open source Python Machine learning library , be based on Torch, For natural language processing applications . be relative to TensorFlow More lightweight , It is more suitable for scientific research and small projects .
Here is a simple example , be based on Fast RCNN Algorithm and PyTorch Fast target recognition . What we use here is coco Data sets have trained online models , It is relatively simple to operate .
Code
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
# Download the trained model
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)
# Change one PIL Library pictures or numpy The array of is tensor Tensor type ; Convert from [0,255]->[0,1]
transform = T.Compose([T.ToTensor()])
img = transform(img)
pred = model([img])
print(pred[0]['labels'].numpy())
# Category extraction
pred_class = [COCO_INSTANCE_CATEGORY_NAMES[i] for i in list(pred[0]['labels'].numpy())]
# Coordinate extraction
pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().numpy())]
# Find the ones that meet the similarity requirements
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)
# Convert to RGB Images
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for i in range(len(boxes)):
# Circle the target according to the coordinates
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)
# Label categories
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")
result
Example 1
Example 2
边栏推荐
- Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
- 前缀和数组系列
- [ 英语 ] 语法重塑 之 英语学习的核心框架 —— 英语兔学习笔记(1)
- [Yu Yue education] flower cultivation reference materials of Weifang Vocational College
- 《从0到1:CTFer成长之路》书籍配套题目(周更)
- Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- Basic commands of MySQL
- ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
- Use shortcut LNK online CS
猜你喜欢
【刷题】怎么样才能正确的迎接面试?
云上有AI,让地球科学研究更省力
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
漏了监控:Zabbix对Eureka instance状态监控
【软件测试进阶第1步】自动化测试基础知识
Leetcode daily question (971. flip binary tree to match preorder traversal)
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
前缀和数组系列
(practice C language every day) reverse linked list II
How to translate professional papers and write English abstracts better
随机推荐
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
Reflex WMS medium level series 3: display shipped replaceable groups
查询字段个数
Database basics exercise part 2
我的创作纪念日
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
万丈高楼平地起,每个API皆根基
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Day 248/300 关于毕业生如何找工作的思考
Number of query fields
How effective is the Chinese-English translation of international economic and trade contracts
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
一文读懂简单查询代价估算
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
Pymongo gets a list of data
LeetCode每日一题(1997. First Day Where You Have Been in All the Rooms)
ROS learning_ Basics
基于购买行为数据对超市顾客进行市场细分(RFM模型)
MySQL high frequency interview 20 questions, necessary (important)