如何解决在不同的线程中运行相同的脚本python
我有一个脚本,可以识别相机中的印版,现在我需要相同的脚本来识别其他相机中的图像,因此简而言之,它需要一次从两个相机中识别出来,我正在使用Tensoflow / keras和YOLO对象检测,可以有人建议解决方案,我尝试使用不同的线程,但是我无法启动第二个线程,我将发布我尝试过的内容
import sys,os
import threading
import keras
import cv2
import traceback
import numpy as np
import time
import sqlite3
import pyodbc
import time
from imutils.video import VideoStream
from pattern import apply_pattern
import darknet.python.darknet as dn
from os.path import splitext,basename
from glob import glob
from darknet.python.darknet import detect
from src.label import dknet_label_conversion
from src.utils import nms
from src.keras_utils import load_model
from glob import glob
from os.path import splitext,basename
from src.utils import im2single
from src.keras_utils import load_model,detect_lp
from src.label import Shape,writeShapes
import imutils
cam_vlez ="rtsp://"
cam_izlez = "rtsp://a"
def adjust_pts(pts,lroi):
return pts*lroi.wh().reshape((2,1)) + lroi.tl().reshape((2,1))
def start_vlez(cam):
while True:
cap = VideoStream(cam).start()
start_time = time.time()
sky = cap.read()
frame = sky[100:700,300:1800]
w = frame.shape[0]
h = frame.shape[1]
ratio = float(max(frame.shape[:2])) / min(frame.shape[:2])
side = int(ratio * 288.)
bound_dim = min(side + (side % (2 ** 4)),608)
Llp,LlpImgs,_ = detect_lp(wpod_net,im2single(frame),bound_dim,2**4,(240,80),lp_threshold)
cv2.imshow('detected_plate',frame)
if len(LlpImgs):
Ilp = LlpImgs[0]
s = Shape(Llp[0].pts)
for shape in [s]:
ptsarray = shape.pts.flatten()
try:
frame = cv2.rectangle(frame,(int(ptsarray[0]*h),int(ptsarray[5]*w)),(int(ptsarray[1]*h),int(ptsarray[6]*w)),(0,255,0),3)
cv2.imshow('detected_plate',frame)
except:
traceback.print_exc()
sys.exit(1)
Ilp = cv2.cvtColor(Ilp,cv2.COLOR_BGR2GRAY)
Ilp = cv2.cvtColor(Ilp,cv2.COLOR_GRAY2BGR)
cv2.imwrite('%s/_lp.png' % (output_dir),Ilp*255.)
cv2.imshow('lp_bic',Ilp)
R,(width,height) = detect(ocr_net,ocr_meta,'lp_images/_lp.png',thresh=ocr_threshold,nms=None)
if len(R):
L = dknet_label_conversion(R,width,height)
L = nms(L,.45)
L.sort(key=lambda x: x.tl()[0])
lp_str = ''.join([chr(l.cl()) for l in L])
result =apply_pattern(lp_str)
write_to_database(result)
print("License Plate Detected: ",lp_str)
print("Written in database: ",result)
print("--- %s seconds ---" % (time.time() - start_time))
#updateSqliteTable(lp_str)
def start_izlez(cam):
while True:
cap = VideoStream(cam).start()
start_time = time.time()
sky = cap.read()
frame = sky[100:700,lp_threshold)
cv2.imshow('detected_plate1',3)
cv2.imshow('detected_plate1',.45)
L.sort(key=lambda x: x.tl()[0])
lp_str = ''.join([chr(l.cl()) for l in L])
result =apply_pattern(lp_str)
write_to_database(result)
print("License Plate Detected: ",result)
print("--- %s seconds ---" % (time.time() - start_time))
#updateSqliteTable(lp_str)
if __name__ == '__main__':
try:
output_dir = 'lp_images/'
lp_threshold = .5
wpod_net_path = "./my-trained-model/my-trained-model1_final.json"
wpod_net = load_model(wpod_net_path)
ocr_threshold = .6
ocr_weights = b'data/ocr/ocr-net.weights'
ocr_netcfg = b'data/ocr/ocr-net.cfg'
ocr_dataset = b'data/ocr/ocr-net.data'
ocr_net = dn.load_net(ocr_netcfg,ocr_weights,0)
ocr_meta = dn.load_meta(ocr_dataset)
t = threading.Thread(target=start_vlez(cam_izlez))
t1 = threading.Thread(target=start_izlez(cam_vlez))
t.start()
t1.start()
except:
print ("Error: unable to start thread")
解决方法
target=
中的 Thread
需要不带()
和参数的函数名称-稍后它将使用()
来启动它。
您当前的代码未在线程中运行函数,但其工作原理类似于
result = start_vlez(cam_izlez)
result1 = start_izlez(cam_vlez)
t = threading.Thread(target=result)
t1 = threading.Thread(target=result1)
t.start()
t2.start()
因此它在主线程中运行第一个函数,并等待其结束。接下来,它也在主线程中运行第二个函数,并等待其结束。然后,它尝试使用Thread
如果有参数,则需要在()
中使用不带target=
的函数名,并在args=
中使用带参数的元组
t = threading.Thread(target=start_vlez,args=(cam_izlez,))
t1 = threading.Thread(target=start_izlez,args=(cam_vlez,))
args=
即使对于单个参数也需要元组,因此我在,
和(cam_izlez,)
中使用(cam_vlez,)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。