学习目标:
提示:骨架识别有别的依赖,没有参考价值。如果只是想实现多进程同时实现文字识别和二维码识别的可以随意看看。
python 多进程同时输入一张图,实现骨架识别、文字识别和二维码识别
学习内容:
输入的图像得既包含人 又包含二维码文字,我就随便网上找了个二维码,然后直接电脑截图了
代码:
import multiprocessing
import time
import util as cm
import argparse
import os
import easyocr
import cv2
import task2
import SkeletonTrackingImage
from skeletontracker import skeletontracker
def OCR(PATH):
reader = easyocr.Reader(['ch_sim', 'en'], gpu=False) # 只需要运行一次就可以将模型加载到内存中
result = reader.readtext(PATH)
print(result)
for i in result:
word = i[1]
print(word)
def QR(PATH):
frame = cv2.imread(PATH)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
im = task2.decodeDisplay(gray)
cv2.waitKey(5)
cv2.imshow("Detect", im)
cv2.waitKey(1000)
print("SUCCESS QR DETECT!")
cv2.destroyAllWindows()
def GetJoints(PATH):
args = SkeletonTrackingImage.parser.parse_args()
img = cv2.imread(PATH)
skeletrack = skeletontracker()
skeletons = skeletrack.track_skeletons(img)
cm.render_result(skeletons, img, args.confidence_threshold)
print("Detected skeletons: ", len(skeletons))
print(skeletons)
isSaved = cv2.imwrite("output.jpg", img)
if isSaved:
print("The result image is saved in: ", "output.jpg")
else:
print("Saving the result image failed for the given path: ", "output.jpg")
if args.verbose:
print(skeletons)
if args.output_image:
isSaved = cv2.imwrite(args.output_image, img)
if isSaved:
print("The result image is saved in: ", args.output_image)
else:
print("Saving the result image failed for the given path: ", args.output_image)
if __name__ == '__main__':
start = time.clock()
PATH="people+QR.jpg"
OCR_process = multiprocessing.Process(target=OCR, args=(PATH,))
QR_process = multiprocessing.Process(target=QR, kwargs={'PATH':PATH}) # target
GetJoints_process = multiprocessing.Process(target=GetJoints, kwargs={'PATH': PATH})
#启动进程
OCR_process.start()
QR_process.start()
GetJoints_process.start()
end = time.clock()
print('Running test time: %s Seconds' % (end - start))
输出结果:
就是三个程序同时跑
QR输出:
[INFO] Found QRCODE barcode: http://weixin.qq.com/q/02laFq85IFfSD10000007b
SUCCESS QR DETECT!
OCR输出:
Using CPU. Note: This module is much faster with a GPU.
[([[447, 19], [469, 19], [469, 39], [447, 39]], '十', 0.9694064232314226), ([[548, 200], [614, 200], [614, 224], [548, 224]], 'J飞桨', 0.04646419104456546)]
十
J飞桨
骨架识别输出:
Initialising the Skeleton Tracking
Detected skeletons: 1
[SkeletonKeypoints(joints=[Coordinate(x=159.10357666015625, y=151.125), Coordinate(x=165.5976104736328, y=180.375), Coordinate(x=139.6215057373047, y=183.625), Coordinate(x=136.37449645996094, y=222.625), Coordinate(x=146.11553955078125, y=261.625), Coordinate(x=185.07968139648438, y=180.375), Coordinate(x=185.07968139648438, y=222.625), Coordinate(x=172.0916290283203, y=255.125), Coordinate(x=149.362548828125, y=258.375), Coordinate(x=159.10357666015625, y=316.875), Coordinate(x=168.84461975097656, y=365.625), Coordinate(x=178.5856475830078, y=258.375), Coordinate(x=185.07968139648438, y=313.625), Coordinate(x=188.32669067382812, y=368.875), Coordinate(x=155.8565673828125, y=147.875), Coordinate(x=165.5976104736328, y=147.875), Coordinate(x=149.362548828125, y=147.875), Coordinate(x=172.0916290283203, y=147.875)], confidences=[0.8960610628128052, 0.8762086033821106, 0.8915107250213623, 0.8231115937232971, 0.927017092704773, 0.8750684261322021, 0.8468186855316162, 0.8317177295684814, 0.845022439956665, 0.9005998969078064, 0.8683494329452515, 0.7987264394760132, 0.8966251611709595, 0.9470148086547852, 0.8775635957717896, 0.8433065414428711, 0.7787574529647827, 0.7931344509124756], id=1, id_confirmed_on_cloud=False)]
The result image is saved in: output.jpg
由于他们是同时进行的,所以运行栏里其实输出是三个程序杂糅在一起,如下图:
有空看看能不能把输出都集合在一张图上,好看一点。
6.12更新都集合在一张图上(用的单进程):
import multiprocessing
import time
import util as cm
import argparse
import os
import easyocr
import cv2
import task2
import SkeletonTrackingImage
from skeletontracker import skeletontracker
def OCR(frame):
reader = easyocr.Reader(['ch_sim', 'en'], gpu=False) # 只需要运行一次就可以将模型加载到内存中
result = reader.readtext(frame)
print(result)
for i in result:
word = i[1]
print(word)
def QR(frame):
#frame = cv2.imread(PATH)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img = task2.decodeDisplay(gray,frame)
'''
cv2.waitKey(5)
cv2.imshow("Detect", im)
cv2.waitKey(1000)
print("SUCCESS QR DETECT!")
'''
return img
#cv2.destroyAllWindows()
def GetJoints(frame):
args = SkeletonTrackingImage.parser.parse_args()
img = frame
skeletrack = skeletontracker()
skeletons = skeletrack.track_skeletons(img)
cm.render_result(skeletons, img, args.confidence_threshold)
print("Detected skeletons: ", len(skeletons))
print(skeletons)
isSaved = cv2.imwrite("output.jpg", img)
'''
if isSaved:
print("The result image is saved in: ", "output.jpg")
else:
print("Saving the result image failed for the given path: ", "output.jpg")
if args.verbose:
print(skeletons)
if args.output_image:
isSaved = cv2.imwrite(args.output_image, img)
if isSaved:
print("The result image is saved in: ", args.output_image)
else:
print("Saving the result image failed for the given path: ", args.output_image)
'''
return img
if __name__ == '__main__':
while True:
camera = cv2.VideoCapture(0)
ret, frame = camera.read()
frame = cv2.imread("people+QR.jpg")
im = GetJoints(frame)
im = QR(im)
OCR(frame)
cv2.waitKey(5)
cv2.imshow("Detect", im)
cv2.waitKey(100)
#start = time.clock()
#PATH="people+QR.jpg"
#OCR_process = multiprocessing.Process(target=OCR, args=(PATH,))
#QR_process = multiprocessing.Process(target=QR, kwargs={'PATH':PATH}) # target
#GetJoints_process = multiprocessing.Process(target=GetJoints, kwargs={'PATH': PATH})
#启动进程
#OCR_process.start()
#QR_process.start()
#GetJoints_process.start()
#end = time.clock()
#print('Running test time: %s Seconds' % (end - start))