10.python读取终端的rknn模型

发布时间:2024年01月08日

直接读取终端模型的代码如下:

import cv2
import subprocess
import sys
import os
import time
 
 
start=time.time()
p = subprocess.run(['./rknn_yolov5_demo', './model/RK3588/best.rknn', './model/bus.jpg'],capture_output=True).stdout

print("................................")
outputs=str(p).split("@")
for item in outputs:
    print(item)


end=time.time()
print(end-start)

? ? ? ?当然,我们在读取终端模型时,需要获得模型的类及坐标和精度,并且根据自己选中的类对图片中的目标画框并标注出类的名称和精度。检测一张的图片的代码如下(检测视频或者摄像头视频时把图片路径修改成视频取帧的路径即可):
?

import cv2
import subprocess
import sys
import os
import time
import matplotlib.pyplot as plt

start=time.time()
p = subprocess.run(['./rknn_yolov5_demo', './model/RK3588/best.rknn', './model/bus.jpg'],capture_output=True).stdout

print("................................")
outputs=str(p).split("@")
print("====>",len(outputs))

classes=[]
if len(outputs)>3:
    s1=outputs[1].split("\\")[-1][1:]
    s2=outputs[-1].split("\\")[0]
    #print(s1) 
    #print(s2)
    classes.append(s1)
    for i in range(2,len(outputs)-1):
        #print(outputs[i].split("\\"))
        classes.append(outputs[i].split("\\")[0])
        classes.append(outputs[i].split("\\")[1][1:])
    classes.append(s2)
#print("..............................................................")
#print(classes)
num_class=[]
if len(classes)>2:
    for i in range(0,len(classes)-1,2):
        num_class.append(classes[i:i+2])

print("..............................................................")
print(num_class)

if len(num_class)>0:
    img=cv2.imread(r"model/bus.jpg")#读取要检测的图片,后面对该图片标注检测的结果
    for item in num_class:
        index1=item[1].split(")")[0][2:].split(" ")
        index2=item[1].split(")")[1]
        #print(index1,index2)
        
        a=(int(index1[0]),int(index1[1]))
        b=(int(index1[2]),int(index1[3]))
        print(a,b)
        cv2.rectangle(img, a, b, (0, 255, 0), 2)
        
        c=item[0]
        d=float(index2)
        font=cv2.FONT_HERSHEY_SIMPLEX
        imgzi = cv2.putText(img, '{} {:.3f}'.format(c,d), (a[0], a[1]), font, 0.5, (0, 255, 255), 1)  
        
cv2.imwrite('1.jpg',img)#检测结果的图片的保存路径
end=time.time()
print(end-start)

以上代码的标注结果如下:

? ? 测试摄像头视频的代码如下:

# coding:utf-8

import cv2
import subprocess
import sys
import os
import time
import matplotlib.pyplot as plt

def mm():

    start=time.time()
    p = subprocess.run(['./rknn_yolov5_demo', './model/RK3588/best.rknn', './hh.jpg'],capture_output=True).stdout
    
    print("................................")
    outputs=str(p).split("@")
    print("====>",len(outputs))
    
    classes=[]
    if len(outputs)<3:
        classes=[]
    elif len(outputs)==3:
        s1=outputs[1].split("\\")[-1][1:]
        s2=outputs[-1].split("\\")[0]
        classes.append(s1)
        classes.append(s2)
    
    elif len(outputs)>3:
        s1=outputs[1].split("\\")[-1][1:]
        s2=outputs[-1].split("\\")[0]
        #print(s1) 
        #print(s2)
        classes.append(s1)
        for i in range(2,len(outputs)-1):
            #print(outputs[i].split("\\"))
            classes.append(outputs[i].split("\\")[0])
            classes.append(outputs[i].split("\\")[1][1:])
        classes.append(s2)
    #print("..............................................................")
    print(classes)
    num_class=[]
    if len(classes)>0:
        for i in range(0,len(classes)-1,2):
            num_class.append(classes[i:i+2])
    
    print("..............................................................")
    print(num_class)
    img=cv2.imread(r"hh.jpg")
    if len(num_class)>0:
        
        for item in num_class:
            index1=item[1].split(")")[0][2:].split(" ")
            index2=item[1].split(")")[1]
            #print(index1,index2)
            
            a=(int(index1[0]),int(index1[1]))
            b=(int(index1[2]),int(index1[3]))
            print(a,b)
            cv2.rectangle(img, a, b, (0, 255, 0), 2)
            
            c=item[0]
            d=float(index2)
            font=cv2.FONT_HERSHEY_SIMPLEX
            imgzi = cv2.putText(img, '{} {:.3f}'.format(c,d), (a[0], a[1]), font, 0.5, (0, 255, 255), 1)  
            
    cv2.imwrite('1.jpg',img)
    end=time.time()
    print(end-start)
    cv2.imshow("src_image", img)

    cv2.waitKey(1)


cap = cv2.VideoCapture("/dev/video61")

index = 1

while (cap.isOpened()):

    ret, frame = cap.read()
    
    frame = cv2.rotate(frame, 0, dst=None)  # 视频是倒着的,要对视频进行两次90度的翻转
    
    frame = cv2.rotate(frame, 0, dst=None)  # 视频是倒着的,要对视频进行两次90度的翻转
    
    if index%8==0:
        cv2.imwrite("hh.jpg",frame)
        
        start=time.time()
        mm()

        end=time.time()
        print(end-start)
      
    index+=1
    
    #cv2.imshow("src_image", frame)

    #cv2.waitKey(1)
    

文章来源:https://blog.csdn.net/weixin_71719718/article/details/135388543
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。