{"id":670,"date":"2024-03-29T08:00:57","date_gmt":"2024-03-29T00:00:57","guid":{"rendered":"https:\/\/www.zhoubin.asia\/?p=670"},"modified":"2024-03-29T06:39:15","modified_gmt":"2024-03-28T22:39:15","slug":"python-python","status":"publish","type":"post","link":"https:\/\/www.zhoubin.asia\/index.php\/2024\/03\/29\/python-python\/","title":{"rendered":"[Python] python\u4e0a\u4f7f\u7528\u591a\u8fdb\u7a0b\u52a0\u901f\u5b9e\u73b0\u5b9e\u65f6\u76d1\u6d4b\u4e0e\u8ddf\u8e2a"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u9879\u76ee\u7684demo\uff0c\u9700\u8981\u4f7f\u7528\u5230NPU\u7684\u68c0\u6d4b\u6a21\u578b\u68c0\u6d4b\u51fa\u6765\u7ed3\u679c\uff0c\u7136\u540e\u628a\u7ed3\u679c\u518d\u5229\u7528NPU\u505a\u7279\u5f81\u63d0\u53d6\uff0c\u5229\u7528deepsort\u53bb\u505a\u8ffd\u8e2a\u4efb\u52a1\uff0c\u6700\u540e\u663e\u793a\u6700\u7ec8\u7ed3\u679c\u5230\u753b\u9762\u3002<\/p>\n<p>\u56e0\u4e3a\u5ba2\u6237\u8981\u6c42\u7684FPS\u9700\u8981\u8fbe\u5230\u5b9e\u65f6\u663e\u793a\u7684\u6761\u4ef6\uff0c\u800c\u68c0\u6d4b\u4ee5\u53ca\u7279\u5f81\u63d0\u53d6\u9700\u8981\u7684\u7b97\u529b\u6bd4\u8f83\u9ad8\uff0c\u4ecelatency\u7684\u89d2\u5ea6\u6765\u8bf4\uff0c\u4e32\u884c\u8fd0\u884c\u8fd9\u4e9b\u662f\u4e0d\u53ef\u80fd\u8fbe\u5230\u5b9e\u65f6\u7684\u6548\u679c\u7684\u3002<\/p>\n<p>\u90a3\u4e48\u5728Python\u7684\u6846\u67b6\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u591a\u8fdb\u7a0b\u4ee5\u53ca\u5bf9\u5e94\u7684\u5171\u4eab\u5185\u5b58\u548cqueue\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u52a0\u901f\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5b9e\u65f6\u76d1\u6d4b\u4e0e\u8ddf\u8e2a\u7684\u6548\u679c\u3002<\/p>\n<h2>\u5177\u4f53\u5b9e\u73b0\u8fc7\u7a0b<\/h2>\n<ol>\n<li>images_queue\u3001terminate_event\u3001det_ready_event \u548c draw_ready_event \u7528\u4e8e\u8fdb\u7a0b\u95f4\u901a\u4fe1\u548c\u540c\u6b65\u3002<\/li>\n<li>\u5229\u7528det_queue\u4f20\u9012\u68c0\u6d4b\u7ed3\u679c\u4ee5\u53catracking_queue\u4f20\u9012\u6700\u7ec8\u8ffd\u8e2a\u7ed3\u679c\u3002<\/li>\n<li>\u8003\u8651\u5230\u4e0d\u7ba1\u662f\u68c0\u6d4b\u8fd8\u662f\u8ffd\u8e2a\u91cc\u9762\u7684\u7279\u5f81\u63d0\u53d6\u90fd\u9700\u8981\u4f7f\u7528\u7684\u56fe\u50cf\uff0c\u6211\u4eec\u4f7f\u7528frames_shared\u4f5c\u4e3a\u56fe\u50cf\u7684\u5171\u4eab\u5185\u5b58\u6570\u7ec4\uff0c\u7528\u4e8e\u5b58\u50a8\u5e27\u6570\u636e\uff0c\u4ee5\u4fbf\u591a\u4e2a\u8fdb\u7a0b\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<li>\u4e00\u5171\u521b\u5efa\u5e76\u542f\u52a8\u4e86\u4e09\u4e2a\u8fdb\u7a0b\uff1a\n<ol type=\"a\">\n<li>p_npu \u7528\u4e8e\u68c0\u6d4b\u5904\u7406\u3002<\/li>\n<li>p_tracking \u7528\u4e8e\u8ddf\u8e2a\uff08\u5305\u542b\u7279\u5f81\u63d0\u53d6\uff09\u3002<\/li>\n<li>p_frames \u7528\u4e8e\u5e27\u5904\u7406\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\u4e3b\u5faa\u73af\u4ece\u89c6\u9891\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5e27\uff0c\u5c06\u5176\u590d\u5236\u5230\u5171\u4eab\u5185\u5b58\u6570\u7ec4\u4e2d\uff0c\u5e76\u5728\u89c6\u9891\u7ed3\u675f\u65f6\u8bbe\u7f6eterminate_event\u3002<\/li>\n<li>\u6700\u540e\uff0c\u4ee3\u7801\u91ca\u653e\u8d44\u6e90\uff0c\u52a0\u5165\u8fdb\u7a0b\uff0c\u5e76\u5173\u95ed\u4efb\u4f55\u6253\u5f00\u7684\u7a97\u53e3\u3002<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<h2>\u4ee3\u7801\uff1a<\/h2>\n<p><em>from multiprocessing import Process, Queue, Lock, Event<\/em><br \/>\n<em>import cv2<\/em><br \/>\n<em>import queue<\/em><br \/>\n<em>import multiprocessing<\/em><\/p>\n<p><em>video_path = &#8220;VID.mp4&#8221;<\/em><br \/>\n<em>#used for display.<\/em><br \/>\n<em>images_queue = Queue()<\/em><br \/>\n<em>terminate_event = Event()<\/em><br \/>\n<em>det_ready_event = Event()<\/em><br \/>\n<em>draw_ready_event = Event() <\/em><\/p>\n<p><em>det_queue = multiprocessing.Queue()<\/em><br \/>\n<em>tracking_queue = multiprocessing.Queue()<\/em><\/p>\n<p><em>cap = cv2.VideoCapture(video_path)<\/em><br \/>\n<em>width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))<\/em><br \/>\n<em>height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))<\/em><br \/>\n<em>frames_shared = multiprocessing.Array(&#8216;B&#8217;, width * height * 3)<\/em><\/p>\n<p><em>p_npu = multiprocessing.Process(target=npu_process, args=(frames_shared, width, height, det_queue, terminate_event))<\/em><br \/>\n<em>p_tracking = multiprocessing.Process(target=tracking_process, args=(frames_shared, width, height, <\/em><br \/>\n<em>tracking_queue, terminate_event, draw_ready_event))<\/em><br \/>\n<em>p_frames = multiprocessing.Process(target=process_frames, args=(frames_shared, width, height, <\/em><br \/>\n<em>tracking_queue, terminate_event, draw_ready_event))<\/em><br \/>\n<em>p_npu.start()<\/em><br \/>\n<em>p_tracking.start()<\/em><br \/>\n<em>p_frames.start()<\/em><\/p>\n<p><em>while cap.isOpened():<\/em><br \/>\n<em>\u00a0 \u00a0 start_time = time.time()<\/em><br \/>\n<em>\u00a0 \u00a0 ret, frame = cap.read()<\/em><br \/>\n<em>\u00a0 \u00a0 if not ret:<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 break<\/em><br \/>\n<em>\u00a0 \u00a0 frame_shared = np.frombuffer(frames_shared.get_obj(), dtype=np.uint8).reshape(-1, height, width, 3)[0]<\/em><br \/>\n<em>\u00a0 \u00a0 np.copyto(frame_shared, frame)<\/em><\/p>\n<p><em>terminate_event.set()<\/em><br \/>\n<em>cap.release()<\/em><br \/>\n<em>p_npu.join()<\/em><br \/>\n<em>p_tracking.join()<\/em><br \/>\n<em>p_frames.join()<\/em><br \/>\n<em>cv2.destroyAllWindows()<\/em><\/p>\n<p>\u5907\u6ce8\uff1a \u6b64\u65b9\u6848\u5b9e\u73b0\u4e86\u5728x86\u4e0a\u9762\u7684\u5b9e\u65f6\u6027\uff0c\u4f46\u662f\u5f53\u8f6c\u79fb\u5230RK3588\u4e0a\u9762\u7684\u65f6\u5019\uff0c\u6574\u4e2a\u8fc7\u7a0b\u591a\u8fdb\u7a0b\u6df7\u4e71\uff0c\u6574\u4f53\u6548\u679c\u5f88\u5dee\uff0c\u76ee\u524d\u4e0d\u786e\u5b9a\u4f18\u5316\u65b9\u5411\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u9879\u76ee\u7684demo\uff0c\u9700\u8981\u4f7f\u7528\u5230NPU\u7684\u68c0\u6d4b\u6a21\u578b\u68c0\u6d4b\u51fa\u6765\u7ed3\u679c\uff0c\u7136\u540e\u628a\u7ed3\u679c\u518d\u5229\u7528NPU\u505a\u7279\u5f81\u63d0\u53d6\uff0c\u5229\u7528de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-670","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/posts\/670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/comments?post=670"}],"version-history":[{"count":3,"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/posts\/670\/revisions"}],"predecessor-version":[{"id":674,"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/posts\/670\/revisions\/674"}],"wp:attachment":[{"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/media?parent=670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/categories?post=670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhoubin.asia\/index.php\/wp-json\/wp\/v2\/tags?post=670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}