当前位置:网站首页>Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
2022-07-06 20:28:00 【gmHappy】
The main content of this chapter is to use mqtt、 Multithreading 、 The queue implementation model is loaded at one time , Batch image recognition and classification function
The directory structure is as follows :

mqtt Connection and multithreaded queue management
# -*- coding:utf8 -*-
import paho.mqtt.client as mqtt
from multiprocessing import Process, Queue
import images_detect
MQTTHOST = "192.168.3.202"
MQTTPORT = 1883
mqttClient = mqtt.Client()
q = Queue()
# Connect MQTT The server
def on_mqtt_connect():
mqttClient.connect(MQTTHOST, MQTTPORT, 60)
mqttClient.loop_start()
# Message handler
def on_message_come(mqttClient, userdata, msg):
q.put(msg.payload.decode("utf-8")) # Put in queue
print(" Generate news ", msg.payload.decode("utf-8"))
def consumer(q, pid):
print(" Start the process of consumption sequence ", pid)
# Publishing messages in multiple processes requires reinitialization mqttClient
ImagesDetect = images_detect.ImagesDetect()
ImagesDetect.detect(q)
# subscribe News subscription
def on_subscribe():
mqttClient.subscribe("test", 1) # The theme is "test"
mqttClient.on_message = on_message_come # Message arrival processing function
# publish News release
def on_publish(topic, msg, qos):
mqttClient.publish(topic, msg, qos);
def main():
on_mqtt_connect()
on_subscribe()
for i in range(1, 3):
c1 = Process(target=consumer, args=(q, i))
c1.start()
while True:
pass
if __name__ == '__main__':
main()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
Image recognition
images_detect.py
# coding: utf-8
import numpy as np
import os
import sys
import tarfile
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
import cv2
import decimal
import MyUtil
context = decimal.getcontext()
context.rounding = decimal.ROUND_05UP
class ImagesDetect():
def __init__(self):
sys.path.append("..")
MODEL_NAME = 'faster_rcnn_inception_v2_coco_2018_01_28'
MODEL_FILE = MODEL_NAME + '.tar.gz'
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
NUM_CLASSES = 90
tar_file = tarfile.open(MODEL_FILE)
for file in tar_file.getmembers():
file_name = os.path.basename(file.name)
if 'frozen_inference_graph.pb' in file_name:
tar_file.extract(file, os.getcwd())
# ## Load a (frozen) Tensorflow model into memory.
self.detection_graph = tf.Graph()
with self.detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
# ## Loading label map
# Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
self.category_index = label_map_util.create_category_index(categories)
self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents an object detected
self.boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represents the reliability of the detected object .
self.scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
self.classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')
def detect(self, q):
with self.detection_graph.as_default():
config = tf.ConfigProto()
# config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.2
with tf.Session(graph=self.detection_graph, config=config) as sess:
while True:
img_src = q.get()
print('------------start------------' + MyUtil.get_time_stamp())
image_np = cv2.imread(img_src)
# Expand dimensions , Expected for model : [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Perform detection task .
(boxes, scores, classes, num_detections) = sess.run(
[self.boxes, self.scores, self.classes, self.num_detections],
feed_dict={self.image_tensor: image_np_expanded})
# Visualization of test results
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
self.category_index,
use_normalized_coordinates=True,
line_thickness=8)
print('------------end------------' + MyUtil.get_time_stamp())
# cv2.imshow('object detection', cv2.resize(image_np, (800, 600)))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
MyUtil.py
import time
def get_time_stamp():
ct = time.time()
local_time = time.localtime(ct)
data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
data_secs = (ct - int(ct)) * 1000
time_stamp = "%s.%03d" % (data_head, data_secs)
return time_stamp
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
effect :

边栏推荐
- Recyclerview GridLayout bisects the middle blank area
- 系统与应用监控的思路和方法
- 深度学习分类网络 -- ZFNet
- [cloud native and 5g] micro services support 5g core network
- Is it difficult for small and micro enterprises to make accounts? Smart accounting gadget quick to use
- 永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
- Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
- Deep learning classification network -- zfnet
- 【每周一坑】计算100以内质数之和 +【解答】输出三角形
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
猜你喜欢

爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解

Why do novices often fail to answer questions in the programming community, and even get ridiculed?

HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅

5. 無線體內納米網:十大“可行嗎?”問題
![[network planning] Chapter 3 data link layer (3) channel division medium access control](/img/df/dd84508dfa2449c31d72c808c50dc0.png)
[network planning] Chapter 3 data link layer (3) channel division medium access control

使用.Net分析.Net达人挑战赛参与情况
![[DSP] [Part 2] understand c6678 and create project](/img/06/54b1cf1f5b3308fffb4f84dcf7db9b.png)
[DSP] [Part 2] understand c6678 and create project

数字三角形模型 AcWing 1018. 最低通行费
Tencent T2 Daniel explained in person and doubled his job hopping salary

An East SMS login resurrection installation and deployment tutorial
随机推荐
rt-thread i2c 使用教程
【GET-4】
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
Logic is a good thing
“罚点球”小游戏
Notes on beagleboneblack
系统与应用监控的思路和方法
POJ 3207 Ikki' s Story IV – Panda' s Trick (2-SAT)
Boder radius has four values, and boder radius exceeds four values
SQL injection 2
Digital triangle model acwing 1018 Minimum toll
Learn to punch in Web
Groovy basic syntax collation
电子游戏的核心原理
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
看过很多教程,却依然写不好一个程序,怎么破?
5. 无线体内纳米网:十大“可行吗?”问题
Initial experience of addresssanitizer Technology
Rhcsa Road
APS taps home appliance industry into new growth points