-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.py
1782 lines (1598 loc) · 86.9 KB
/
AI.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/python3
# Typical command lines for a full test on 22.04 with yolo8 verification:
"""
wally@Wahine:~$ conda activate yolo8
(yolo8) wally@Wahine:~$ cd AI
(yolo8) wally@Wahine:~/AI$ python AI.py -y8v -nTPU 1 -d 1 -cam 6onvif.txt -rtsp 19cams.rtsp
"""
## appending 2> /dev/null to the start command line helps if you are getting "Invalid UE golomb code" opencv warnings.
## it also gets rid of mouch of the darknet library "chatter" on startup.
'''
4SEP2022wbk -- unify AI_dev.py and AI_yolo.py to produce this AI.py code.
Start project to add yolo7 verification option, and yolo7 AI inference thread
13SEP2022wbk --yolo4_verify option added, Fisheye camera support from TPU.py merged.
Restored support for NCS2, openCV dnn thread, and SSDv1. But these are really
only useful for systems with maybe four cameras or less, and a higher false positive tolerence
unless NVIDIA GPU is available to use -y4v or -y8v. Something like an old "gaming laptop"
with i5 CPU and GTX970 class GPU. OpenVINO CPU often gives better frame rate than NCS2 on i7.
15SEP2022wbk Tested, but bug remains: typing "q key" in openCV window segfaults when -y4v (--yolo4_verify) option is active.
So remove openCV keypress exit option "it hurts when I do this, then don't do it!" But since the design
is expected to not have a live display, sending SIGINT to the process (Ctrl-C in the terminal window)
is the expected way to exit the program.
16SEP2022wbk Make TPU thread follow the Prototype_AI_Thread format.
Unify the thread parameters across all the AI models.
9SEP2023wbk Drop the idea of adding yolo7 and use the newer easier to use and better documented yolo8
https://github.com/ultralytics/ultralytics
30APR2023wbk Seems impossible to get Yolo8 to run on 16.04 becasue of glibc version issues, but darknet Yolo4 runs fine.
2MAY2023wbk Make debug code in in main() and AI threads that lets me see what was being rejected in verification steps be command line parameter.
Lower verifyConf by 0.05 for -y4AI and -y8AI, seem to have too many false negatives with the TPU/OpenVINO verification default value.
'''
#
#
# Historical evolution
### AI_dev.py 13JUL2019wbk
#
### 16APR2021wbk
# Modified TPU support to try the "legacy" edgetpu API and if its not found try the new PyCoral API
# Tested on Ubuntu 20.04 i3-4025 CPU with PyCoral and the new MPCIe TPU module (< half the cost of USB3 TPU)
# Verified on Ubuntu 16.04 i7 desktop using "legacy" edgeTPU API
#
## 10DEC2019wbk
# Increase queue depth to 2, test if queue full, read and discard oldest to make room for newest
#
## 11DEC2019wbk, add PiCamera Module support, change some command argument defaults and names.
## 27DEC2019wbk, tested PiCamera Module support on Pi3B with NCS and OpenVINO:
# ./AI_dev.py -nNCS 1 -pi -ls --> get ~3.8 fps
# And Coral TPU:
# ./AI_dev.py -nTPU 1 -pi -ls --> get ~8.0 fps
#
## 9MAY2020wbk add intermediate yolov4 thread to verify detections.
# ./AI_yolo.py -nTPU 1 -d 1 -rtsp cam/4HD_Amcrest16.rtsp
#
## TODO:
''' 10JUL2022wbk
--> DONE 1) remove NCS/NSC2 and NCS sdkv1 support, NCS2 support restored 13SEP2022.
--> DONE 2) add yolo verification to CPU/GPU threads.
--> DONE 3) remove PiCam support as no PiCamera module runs on a system with Nvidia
GPU, unless PiCameara hardware and software is compatible with Jetson Nano.
A bit of Google suggests the hardware is compatible, but need to use the openCV
gstreamer capture routines instead of the picamera Python module, so my code is useless as is.
14SEP2022wbk
1) Should the blobThreshold be an array with one value for each camera? Not needed so far.
2MAY2023wbk
--> DONE 1) Investigate adding a camera name along with the cameraURL in -cam and -rtsp files.
2) Investigate adding a yolo or other verification AI model using TPU or NCS2 for CUDA incapable systems.
7MAY2023wbk
1) Consider removing NCS2/OpenVINO support, tests with -nt 1 -nNCS 1 -nTPU1 show the vast majority
of detections are wiht the TPU and it is not just that the TPU processes more frames, they just detect
fewer people, even with their thresholds reduced by 0.1
'''
#
#
''' Some performance test results.
On 17-8750H laptop with Nvidia GTX1060:
python3 AI.py -nTPU 1 -y4v -d 1 -cam 6onvif.txt -rtsp 19cams.rtsp
Yielded ~68 fps with 25 cameras for an ~69683 second test run.
There were ~4.7 million frames processed by the TPU with 11280 persons detected.
The yolo4 verification accepted 10953 and rejected 327.
My review suggests almost all the rejections were false negatives, but a fair price to pay
for the near complete rejection of false positives from my bogus detection collection.
One real false positive that was rejected:
an image where a dog was detected as a person, and person walking the dog had not yet entered frame.
On my i9-12900K with GTX 3070 GPU using yolov4-608.cfg darknet model:
conda activate pycoral (or yolo8)
python AI.py -y4AI -d 1 -cam 6onvif.txt -rtsp 19cams.rtsp
I get ~32 fps per second on 25 cameras.
2697289 images processed, 13960 Persons Detected. 2683329 frames with no person.
22592 detections failed zoom-in verification.
Performace is great, so far no false positive detections and seems
to have great detection sensitivity, espcially at night.
I do get segfault crashes from a darknet function
that I've not had any luck tracking down so far. It may be a GPU
memory issue as it seems to correlate with other code using the GPU/display.
At this point it is so frustrating that I may shitcan the Darknet yolo4.
Especially since the Ultralytics yolo8 works so well.
On my i9-12900K with GTX 3070 GPU using yolov8x.pt model
conda activate yolo8
python AI.py -y8AI -d 1 -cam 6onvif.txt -rtsp 18cams.rtsp
I get ~33 fps per second on 24 cameras.
Performace is great, so far and seems to have great detection sensitivity, espcially at night.
One false positive detection of the neighbor's cat by the pool.
It is clear that the "zoom in on detection and re-inference" rejects a large number of false positives.
On a recent test run with i7-8750H and Nvidia GTX1060:
conda activate yolo8
python3 AI.py -d 0 -z -nTPU 1 -cam 6onvif.txt -y8v -rtsp 19cams.rtsp
3878202 images were processed from 25 cameras netting 72.39 inferences/sec (a bit under 3 fps per camera)
11041 Persons Detected with TPU verification, while 29494 TPU detections failed zoom-in verification.
A very large percentage of the "zoom-in and re-inference" detections were plants, pets, and other true negatives.
Yolo8 Verified: 10907, Rejected: 134. A review of the rejects suggests they were all false negatives since
a person was in the image, but this ~1.2% false negative rate is an acceptable price to pay for the near
elimination false positives on my collection of false positives images collected over a couple of years
without the yolo verification step.
'''
# import the necessary packages
import sys
import signal
from imutils.video import FPS
import argparse
import numpy as np
import cv2
import paho.mqtt.client as mqtt
import os
import time
import datetime
import requests
from PIL import Image
from io import BytesIO
# threading stuff
from queue import Queue
from threading import Lock, Thread
# for saving PTZ view maps
import pickle
# *** System Globals
# these are write once in main() and read-only everywhere else, thus don't need syncronization
global QUIT
QUIT=False # True exits main loop and all threads
global Nrtsp
global Nonvif
global Ncameras
global __CamName__
global AlarmMode # would be Notify, Audio, or Idle, Idle mode doesn't save detections
global UImode
global CameraToView
global subscribeTopic
subscribeTopic = "Alarm/#" # topic controller publishes to to set AI operational modes
global Nmqtt
global mqttCamOffset
global inframe
global mqttFrameDrops
global mqttFrames
global mqttCamsOneThread
# this variable to distribute queued data to the AI threads needs syncronization
global nextCamera
nextCamera = 0 # next camera queue for AI threads to use to grab a frame
cameraLock = Lock()
global SSDv1
# globals for thread control
global __onvifThread__
global __rtspThread__
global __fisheyeThread__
global __yolo4Verify__
global __yolo8Verify__
global __yolo4Thread__
global __yolo8Thread__
global GRID_SIZE
global CLIP_LIMIT
global CLAHE
global __DEBUG__
__DEBUG__ = False
# *** constants for MobileNet-SSD & MobileNet-SSD_V2 AI models
# frame dimensions should be sqaure for MobileNet-SSD
PREPROCESS_DIMS = (300, 300)
if 1:
# *** get command line parameters
# construct the argument parser and parse the arguments for this module
ap = argparse.ArgumentParser()
# enable zoom and verify using yolo inference (requires Nvidia cuda capable video card.
ap.add_argument("-y4v", "--yolo4_verify", action="store_true", help="Verify detection with darknet yolov4 inference on zoomed region")
ap.add_argument("-y8v", "--yolo8_verify", action="store_true", help="Verify detection with a yolov8 inference on zoomed region")
# OpenVINO GPU yolo4 verification
ap.add_argument("-y4ovv", "--yolo4ov_verify", action="store_true", help="Verify detection with OpenVINO yolo4 inference on zoomed region")
ap.add_argument("-myriad", "--MYRIAD", action="store_true", help="Verify detection with OpenVINO yolo4 using NCS2 instead of GPU")
ap.add_argument("-yvq", "--YoloVQ", type=int, default=10, help="Depth of YOLO verification queue, should be about YOLO framerate, default=10")
ap.add_argument("-rq", "--resultsQ", type=int, default=10, help="Minimum Depth of results queue, default=10")
# enable a yolo thread for inference.
ap.add_argument("-y4AI", "--yolov4", action="store_true", help="enable darknet yolov4 inference thread")
ap.add_argument("-y8AI", "--yolov8", action="store_true", help="enable ultralytics yolov8 inference thread")
# parameters that might be installation dependent
ap.add_argument("-c", "--confidence", type=float, default=0.70, help="detection confidence threshold")
ap.add_argument("-vc", "--verifyConfidence", type=float, default=0.80, help="detection confidence for verification")
ap.add_argument("-yvc", "--yoloVerifyConfidence", type=float, default=0.75, help="detection confidence for verification")
ap.add_argument("-blob", "--blobFilter", type=float, default=0.33, help="reject detections that are more than this fraction of the frame")
# specify number of Coral TPU sticks
ap.add_argument("-nTPU", "--nTPU", type=int, default=0, help="number of Coral TPU devices")
# use one mqtt thread for all cameras instead of one mqtt thread per mqtt camera
ap.add_argument("-mqttMode", "--mqttCamOneThread", action="store_true", help="Use one mqtt thread for all mqtt cameras")
ap.add_argument("-mqttDemand", "--mqttDemand", action="store_true", help="Use sendOne/N handshake for MQTT cameras")
# number of software (CPU only) AI threads, always have one thread per installed NCS stick
ap.add_argument("-nt", "--nAIcpuThreads", type=int, default=0, help="0 --> no CPU AI thread, >0 --> N threads")
ap.add_argument("-GPU", "--useGPU", action="store_true", help="use GPU instead of CPU AI thread, forces N threads == 1")
# must specify number of NCS sticks for OpenVINO, trying load in a try block and error, wrecks the system!
ap.add_argument("-nNCS", "--nNCS", type=int, default=0, help="number of Myraid devices")
# use Mobilenet-SSD Caffe model instead of Tensorflow Mobilenet-SSDv2_coco
ap.add_argument("-SSDv1", "--SSDv1", action="store_true", help="Use original Mobilenet-SSD Caffe model for NCS & OVcpu")
# specify text file with list of URLs for camera rtsp streams
ap.add_argument("-rtsp", "--rtspURLs", default="cameraURL.rtsp", help="path to file containing rtsp camera stream URLs")
# specify text file with list of URLs cameras http "Onvif" snapshot jpg images
ap.add_argument("-cam", "--cameraURLs", default="cameraURL.txt", help="path to file containing http camera jpeg image URLs")
# display mode, mostly for test/debug and setup, general plan would be to run "headless"
ap.add_argument("-d", "--display", type=int, default=1,
help="display images on host screen, 0=no display, 1=live display")
# specify MQTT broker
ap.add_argument("-mqtt", "--mqttBroker", default="localhost", help="name or IP of MQTT Broker")
# specify MQTT broker for camera images via MQTT, if not "localhost"
ap.add_argument("-camMQTT", "--mqttCameraBroker", default="localhost", help="name or IP of MQTTcam/# message broker")
# number of MQTT cameras published as Topic: MQTTcam/N, subscribed here as Topic: MQTTcam/#, Cams numbered 0 to N-1
ap.add_argument("-Nmqtt", "--NmqttCams", type=int, default=0,
help="number of MQTT cameras published as Topic: MQTTcam/N, Cams numbered 0 to N-1")
# alternate, specify a list of camera numbers
ap.add_argument("-camList", "--mqttCamList", type=int, nargs='+',
help="list of MQTTcam/N subscription topic numbers, cam/N numbered from 0 to Nmqtt-1.")
# specify display width and height
ap.add_argument("-dw", "--displayWidth", type=int, default=3840, help="host display Width in pixels, default=1920")
ap.add_argument("-dh", "--displayHeight", type=int, default=2160, help="host display Height in pixels, default=1080")
# specify host display width and height of camera image
ap.add_argument("-iw", "--imwinWidth", type=int, default=640, help="camera host display window Width in pixels, default=640")
ap.add_argument("-ih", "--imwinHeight", type=int, default=360, help="camera host display window Height in pixels, default=360")
# specify file path of location to same detection images on the localhost
ap.add_argument("-sp", "--savePath", default="", help="path to location for saving detection images, default ../detect")
# save all processed images, fills disk quickly, really slows things down, but useful for test/debug
## CLAHE parameters
ap.add_argument("-cl", "--ClipLimit", type=float, default=4.5, help="CLAHE clipLimit parameter, default=4.5")
ap.add_argument("-gs", "--GridSize", type=int, default=5, help="CLAHE tileGridSize parameter, default=5")
ap.add_argument("-clahe", "--CLAHE", action="store_true", help="Enable CLAHE contrast enhancement on zoomed detection")
# debug visulize verification rejections
ap.add_argument("-dbg", "--debug", action="store_true", help="enable debug display of verification failures")
# show zoom image of detections even if -d parameter is 0
ap.add_argument("-z", "--zoom", action="store_true", help="always display zoomed image of detection.")
# Disable local save of detections on AI host -nls is same as -nsz and -nsf options
ap.add_argument("-nls", "--NoLocalSave", action="store_true", help="no saving of detection images on local AI host")
# don't save zoomed image locally
ap.add_argument("-nsz", "--NoSaveZoom", action="store_true", help="don't locally save zoomed detection image")
# don't save full images locally
ap.add_argument("-nsf", "--NoSaveFull", action="store_true", help="don't locally save full detection frame.")
# send full frame image of detections to node-red instead of zoomed in on detection
ap.add_argument("-nrf", "--nodeRedFull", action="store_true", help="full frame detection images to node-read instead of zoom images")
args = vars(ap.parse_args())
mqttCamsOneThread = args["mqttCamOneThread"]
MQTTdemand = args["mqttDemand"]
# mark start of this code in log file
print("$$$**************************************************************$$$")
currentDT = datetime.datetime.now()
print("*** " + currentDT.strftime(" %Y-%m-%d %H:%M:%S") + " ***")
print("[INFO] using openCV-" + cv2.__version__)
# *** Function definitions
#**********************************************************************************************************************
#**********************************************************************************************************************
#**********************************************************************************************************************
# Boilerplate code to setup signal handler for graceful shutdown on Linux
def sigint_handler(signal, frame):
global QUIT
currentDT = datetime.datetime.now()
#print('caught SIGINT, normal exit. -- ' + currentDT.strftime("%Y-%m-%d %H:%M:%S"))
QUIT=True
def sighup_handler(signal, frame):
global QUIT
currentDT = datetime.datetime.now()
print('caught SIGHUP! ** ' + currentDT.strftime("%Y-%m-%d %H:%M:%S"))
QUIT=True
def sigquit_handler(signal, frame):
global QUIT
currentDT = datetime.datetime.now()
print('caught SIGQUIT! *** ' + currentDT.strftime("%Y-%m-%d %H:%M:%S"))
QUIT=True
def sigterm_handler(signal, frame):
global QUIT
currentDT = datetime.datetime.now()
print('caught SIGTERM! **** ' + currentDT.strftime("%Y-%m-%d %H:%M:%S"))
QUIT=True
signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGHUP, sighup_handler)
signal.signal(signal.SIGQUIT, sigquit_handler)
signal.signal(signal.SIGTERM, sigterm_handler)
#**********************************************************************************************************************
## MQTT callback functions
##
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
global subscribeTopic
#print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed. -- straight from Paho-Mqtt docs!
client.subscribe(subscribeTopic)
# The callback for when a PUBLISH message is received from the server, aka message from SUBSCRIBE topic.
def on_message(client, userdata, msg):
global AlarmMode # would be Notify, Audio, or Idle, Idle mode doesn't save detections
global UImode
global CameraToView
if str(msg.topic) == "Alarm/MODE": # Idle will not save detections, Audio & Notify are the same here
currentDT = datetime.datetime.now() # logfile entry
AlarmMode = str(msg.payload.decode('utf-8'))
print(str(msg.topic)+": " + AlarmMode + currentDT.strftime(" ... %Y-%m-%d %H:%M:%S"))
return
# UImode: 0->no Dasboard display, 1->live image from selected cameram 2->detections from selected camera, 3->detection from any camera
if str(msg.topic) == "Alarm/UImode": # dashboard control Disable, Detections, Live exposes apparent node-red websocket bugs
currentDT = datetime.datetime.now() # especially if browser is not on localhost, use sparingly, useful for camera setup.
print(str(msg.topic)+": " + str(int(msg.payload)) + currentDT.strftime(" ... %Y-%m-%d %H:%M:%S"))
UImode = int(msg.payload)
return
if str(msg.topic) == "Alarm/ViewCamera": # dashboard control to select image to view
currentDT = datetime.datetime.now()
print(str(msg.topic)+": " + str(int(msg.payload)) + currentDT.strftime(" ... %Y-%m-%d %H:%M:%S"))
CameraToView = int(msg.payload)
return
def on_publish(client, userdata, mid):
#print("mid: " + str(mid)) # don't think I need to care about this for now, print for initial tests
pass
def on_disconnect(client, userdata, rc):
if rc != 0:
currentDT = datetime.datetime.now()
print("Unexpected MQTT disconnection!" + currentDT.strftime(" ... %Y-%m-%d %H:%M:%S "), client)
pass
# callbacks for mqttCam that can't be shared
# mqttCamsOneThread=False is default
## True/False no significant difference on i7-6700K Desktop both ~53 fps for 15 ~5 fps MQTTcams from i5ai rtsp2mqtt server
## On Pi4, XU-4 etc. one thread for all mqttCams is ~1.5 fps faster.
if not mqttCamsOneThread: # use one mqtt thread per mqttCam
# callbacks for mqttCam that can't be shared
def on_mqttCam_connect(client, userdata, flags, rc):
camT=userdata[0]
camN=userdata[1]
client.subscribe("MQTTcam/"+str(camT), 0)
def on_mqttCam(client, userdata, msg):
global mqttCamOffset
global inframe
global mqttFrameDrops
global mqttFrames
# put input image into the camera's inframe queue
try:
camT=userdata[0]
camN=userdata[1]
mqttFrames[camN]+=1
# thanks to @krambriw on the node-red user forum for clarifying this for me
npimg=np.frombuffer(msg.payload, np.uint8) # convert msg.payload to numpy array
frame = cv2.imdecode(npimg, cv2.IMREAD_COLOR) # decode image file into openCV image
imageDT=datetime.datetime.now()
if inframe[camN+mqttCamOffset].full():
[_,_,_]=inframe[camN+mqttCamOffset].get(False)
mqttFrameDrops[camN]+=1 # is happes here, shouldn't happen below
inframe[camN+mqttCamOffset].put((frame, camN+mqttCamOffset, imageDT), False)
##inframe[camN+mqttCamOffset].put((frame, camN+mqttCamOffset), True, 0.200)
except:
mqttFrameDrops[camN]+=1 # queue.full() is not 100% reliable
if MQTTdemand:
client.publish(str("sendOne/" + str(camT)), "", 0, False)
## time.sleep(0.001) # force thread dispatch, hard to tell if this helps or not.
return
else:
def on_mqttCam_connect(client, camList, flags, rc):
for camN in camList:
client.subscribe("MQTTcam/"+str(camN), 0)
def on_mqttCam(client, camList, msg):
global mqttCamOffset
global inframe
global mqttFrameDrops
global mqttFrames
global Nmqtt ## eliminate len(camList) call by using global
if msg.topic.startswith("MQTTcam/"):
camNstr=msg.topic[len("MQTTcam/"):] # get camera number as string
if camNstr.isdecimal():
camT = int(camNstr)
if camT not in camList:
currentDT = datetime.datetime.now()
print("[Error! Invalid MQTTcam Camera number: " + str(camT) + currentDT.strftime(" ... %Y-%m-%d %H:%M:%S"))
return
for i in range(Nmqtt):
if camT == camList[i]:
camN=i
break
else:
currentDT = datetime.datetime.now()
print("[Error! Invalid MQTTcam message sub-topic: " + camNstr + currentDT.strftime(" ... %Y-%m-%d %H:%M:%S"))
return
# put input image into the camera's inframe queue
try:
mqttFrames[camN]+=1
# thanks to @krambriw on the node-red user forum for clarifying this for me
npimg=np.frombuffer(msg.payload, np.uint8) # convert msg.payload to numpy array
frame = cv2.imdecode(npimg, cv2.IMREAD_COLOR) # decode image file into openCV image
imageDT=datetime.datetime.now()
if inframe[camN+mqttCamOffset].full():
[_,_,_]=inframe[camN+mqttCamOffset].get(False)
mqttFrameDrops[camN]+=1 # is happes here, shouldn't happen below
inframe[camN+mqttCamOffset].put((frame, camN+mqttCamOffset, imageDT), False)
except:
mqttFrameDrops[camN]+=1 # queue.full() is not 100% reliable
try:
if MQTTdemand:
client.publish(str("sendOne/" + str(camT)), "", 0, False)
## time.sleep(0.001) # force thread dispatch, hard to tell if this helps or not.
except Exception as e:
print("pub error " + str(e))
return
'''
# Hard to believe but Python threads don't have a terminate signal, need a kludge like this
# There are other ways, but I want some stats printed at thread termination.
# I think the real issue is the AI threads are in seperate Python files and its a scope issue for the Quit global
def QUITf():
global QUIT
return QUIT
'''
# *** main()
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
def main():
global QUIT
global AlarmMode # would be Notify, Audio, or Idle, Idle mode doesn't save detections
AlarmMode="Audio" # will be Email, Audio, or Idle via MQTT controller from alarmboneServer
global CameraToView
CameraToView=0
global UImode
UImode=0 # controls if MQTT buffers of processed images from selected camera are sent as topic: ImageBuffer
global subscribeTopic
global Nonvif
global Nrtsp
global Nmqtt
global mqttCamOffset
global mqttFrameDrops
global inframe
global Ncameras
global __CamName__
global CamName
global mqttFrames
global mqttCamsOneThread
global __PYCORAL__
# globals for thread control, maybe QUITf() was cleaner, but I can stage the stopping for better [INFO} reporting on exit
global __darknetThread__
global __rtspThread__
global __fisheyeThread__
# command line "store true" flags
global __yolo4Verify__
##global __yolo7Verify__
##global __yolo4Thread__
global SSDv1
global GRID_SIZE
global CLIP_LIMIT
global CLAHE
global __DEBUG__
# set variables from command line auguments or defaults
#$$$# active TPU
nCoral = args["nTPU"]
if nCoral > 1:
nCoral = 1 # Not finished multiple TPU support, not sure it is needed or will be useful.
nCPUthreads = args["nAIcpuThreads"]
# It appears that Intel GPU and CUDA cannot be used together.
useGPU = args["useGPU"]
if useGPU and nCPUthreads !=1:
nCPUthreads=1
nNCSthreads = args["nNCS"] # the same thread function is used for OpenVINO CPU and NCS/NCS2 threads, my naming could be better.
SSDv1 = args["SSDv1"]
if SSDv1 and nNCSthreads >0:
print("[INFO] NCS2 does not support SSDv1 Caffe model, switching to CPU model.")
nNCSthreads = 0
nCPUthreads += 1
confidence = args["confidence"]
verifyConf = args["verifyConfidence"]
yoloVerifyConf = args["yoloVerifyConfidence"]
blobThreshold = args["blobFilter"]
MQTTcameraServer = args["mqttCameraBroker"]
Nmqtt = args["NmqttCams"]
camList=args["mqttCamList"]
if camList is not None:
Nmqtt=len(camList)
elif Nmqtt>0:
camList=[]
for i in range(Nmqtt):
camList.append(i)
dispMode = args["display"]
if dispMode > 1:
displayMode=1
CAMERAS = args["cameraURLs"]
RTSP = args["rtspURLs"]
MQTTserver = args["mqttBroker"] # this is for command and control messages, and detection messages
displayWidth = args["displayWidth"]
displayHeight = args["displayHeight"]
imwinWidth = args["imwinWidth"]
imwinHeight = args["imwinHeight"]
savePath = args["savePath"]
NoLocalSave = args["NoLocalSave"]
NoSaveZoom = args["NoSaveZoom"]
NoSaveFull = args["NoSaveFull"]
nodeRedFull= args["nodeRedFull"]
__DEBUG__ = args["debug"]
show_zoom = args["zoom"]
# *** setup path to save AI detection images
if savePath == "":
home, _ = os.path.split(os.getcwd())
detectPath = home + "/detect"
if os.path.exists(detectPath) == False:
os.mkdir(detectPath)
else:
detectPath=savePath
if os.path.exists(detectPath) == False:
print(" Path to location to save detection images must exist! Exiting ...")
quit()
yolo4_verify=args["yolo4_verify"]
OVyolo4_verify=args["yolo4ov_verify"]
OVmyriad=args["MYRIAD"]
yoloVQdepth=args["YoloVQ"]
if OVmyriad is True:
yoloVQdepth=3 # NCS2 has ~2 fps frame rate with YOLO
resultsQdepth=args["resultsQ"]
yolo8_verify=args["yolo8_verify"]
yolo4AI=args["yolov4"]
yolo8AI=args["yolov8"]
# 24PR2023wbk: Screen for incompatable options and force sane behavior.
# such as only one of -y4v or -y8v can be used.
# only one of -y4AI or -y8AI can be used.
# -y4v or -y8v can not be used if either -y4AI or -y8AI is active.
# Need to test if TPU, CPU, NCS2, can be mixed with one of -y4AI or -y8AI.
if yolo4AI and yolo8AI:
yolo4AI = False
print("[WARN] Only one of -y4AI or -y8AI can be used. Forcing -y8AI")
if yolo4_verify and yolo8_verify and OVyolo4_verify:
yolo4_verify=False
OVyolo4_verify = False
print("[WARN] Only one of -y4v or -y4ovv or -y8v can be used. Forcing -y8v")
if yolo4AI or yolo8AI:
yolo4_verify = False
yolo8_verify = False
OVyolo4_verify = False
if useGPU and (yolo4_verify or yolo8_verify or yolo4AI or yolo8AI):
useGPU=False
nCPUthreads=1 # does multiple GPU threads make sense? I make no attempt allow both GPU and CPU threads
print("[WARN] Intel openCL GPU can not be used with CUDA, using CPU AI thread instead.")
# init CLAHE
CLAHE = args["CLAHE"]
if CLAHE:
GRID_SIZE = (args["GridSize"],args["GridSize"])
CLIP_LIMIT = args["ClipLimit"]
clahe = cv2.createCLAHE(CLIP_LIMIT,GRID_SIZE)
# *** get Onvif camera URLs
# cameraURL.txt file can be created by first running the nodejs program (requires node-onvif be installed):
# nodejs onvif_discover.js
#
# This code does not really use any Onvif features, Onvif compatability is useful to "automate" getting URLs used to grab snapshots.
# Any camera that returns a jpeg image from a web request to a static URL should work.
CamName=list() # dynamically built list of camera names read from file or created as Cam0, Cam1, ... CamN
try:
#CameraURL=[line.rstrip() for line in open(CAMERAS)] # force file not found
#Nonvif=len(CameraURL)
l=[line.split() for line in open(CAMERAS)]
CameraURL=list()
for i in range(len(l)):
CameraURL.append(l[i][0])
if len(l[i]) > 1:
CamName.append(l[i][1])
else:
CamName.append("Cam" + str(i))
Nonvif=len(CameraURL)
print("[INFO] " + str(Nonvif) + " http Onvif snapshot threads will be created.")
except Exception as e:
# No Onvif cameras
#print(e)
print("[INFO] No " + str(CAMERAS) + " file. No Onvif snapshot threads will be created.")
Nonvif=0
Ncameras=Nonvif
#print(CamName)
# *** get rtsp URLs
try:
#rtspURL=[line.rstrip() for line in open(RTSP)]
#Nrtsp=len(rtspURL)
rtspURL=list()
l=[line.split() for line in open(RTSP)]
for i in range(len(l)):
rtspURL.append(l[i][0])
if len(l[i]) > 1:
CamName.append(l[i][1])
else:
CamName.append("Cam" + str(i+Ncameras))
Nrtsp=len(rtspURL)
print("[INFO] " + str(Nrtsp) + " rtsp stream threads will be created.")
except:
# no rtsp cameras
print("[INFO] No " + str(RTSP) + " file. No rtsp stream threads will be created.")
Nrtsp=0
Ncameras+=Nrtsp
# define fisheye cameras and virtual PTZ views
# fisheye.rtsp is expected to be created with the interactive fisheye_window C++ utility program
try:
l=[line.rstrip() for line in open('fisheye.rtsp')]
FErtspURL=list()
PTZparam=list()
j=-1
for i in range(len(l)):
if not l[i]: continue
if l[i].startswith('rtsp'):
FErtspURL.append(l[i])
j+=1
PTZparam.append([])
else:
PTZparam[j].append(l[i].strip().split(' '))
print("[INFO] Setting up PTZ virtual cameras views from fisheye camera ...")
#print(FErtspURL)
#print(PTZparam)
Nfisheye=len(FErtspURL) # modified rtsp thread will send PTZ views to seperate queues, this is number of fisheye threads
NfeCam=0 # total number of queues to be created for virtual PTZ cameras
for i in range(Nfisheye):
if len(PTZparam[i])<2 or len(PTZparam[i][0])<2 or len(PTZparam[i][1])!=6:
# this is where Python's features make code simple but obtuse!
# setting up this data structure in C/C++ gives me cooties with the variable number of possible PTZ views per camera!
print('[ERROR] PTZparam[' + str(i) + '] must contain [srcW, srcH],[dstW,detH, alpha,beta,theta,zoom] entries, Exiting ...')
quit()
NfeCam += len(PTZparam[i])-1 # the first entry is camera resolution, not a PTZ view
# I'm not bothering with naming fisheye camera views, just create sequential names
for i in range(NfeCam):
CamName.append("FEview" + str(i))
except:
# no fisheye cameras
print("[INFO] No fisheye.rtsp file. No fisheye camera rtsp stream threads will be created.")
NfeCam=0
Nfisheye=0
FishEyeOffset=Ncameras
Ncameras+=NfeCam # add fisheye virtual PTZ views to cameras count
mqttCamOffset = Ncameras
mqttFrameDrops = 0
mqttFrames = 0
if Nmqtt > 0:
print("[INFO] allocating " + str(Nmqtt) + " MQTT image queues...")
# Again not trying to name MQTT cams, may remove this eventually or use IMGMQ transport instead
for i in range(Nmqtt):
CamName.append("MQTT" + str(i))
Ncameras+=Nmqtt # I generally expect Nmqtt to be zero if Ncameras is not zero at this point, but its not necessary
if Ncameras == 0:
print("[INFO] No Cameras, rtsp Streams, or MQTT image inputs specified! Exiting...")
quit()
# *** allocate queues
print("[INFO] allocating camera and stream image queues...")
# we simply make one queue for each camera, rtsp stream, and MQTTcamera
QDEPTH = 3 # Make queue depth be three, sometimes get two frames less then 20 mS appart with
# "read queue if full and then write frame to queue" in camera input thread
## QDEPTH = 2 # bump up for trial of "read queue if full and then write to queue" in camera input thread
## QDEPTH = 1 # small values improve latency
results = Queue(max(resultsQdepth,Ncameras))
inframe = list()
for i in range(Ncameras):
inframe.append(Queue(QDEPTH))
if yolo4_verify or yolo8_verify or OVyolo4_verify:
###yoloQ = Queue(max(10,Ncameras)) # this can lead to very long latencies if the AI thread is much faster than the yolo verification thread.
yoloQ = Queue(yoloVQdepth) # This should be approx the lessor of the AI thread frame rate and yolo verification frame rate
else:
yoloQ = None
# build grey image for mqtt windows
img = np.zeros(( imwinHeight, imwinWidth, 3), np.uint8)
img[:,:] = (127,127,127)
retv, img_as_jpg = cv2.imencode('.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
# *** setup display windows if necessary
# mostly for initial setup and testing, not worth a lot of effort at the moment
if dispMode > 0:
if Nonvif > 0:
print("[INFO] setting up Onvif camera image windows ...")
for i in range(Nonvif):
name=str("Live_" + CamName[i])
cv2.namedWindow(name, flags=cv2.WINDOW_GUI_NORMAL + cv2.WINDOW_AUTOSIZE)
cv2.waitKey(1)
if Nrtsp > 0:
print("[INFO] setting up rtsp camera image windows ...")
for i in range(Nrtsp):
name=str("Live_" + CamName[i+Nonvif])
cv2.namedWindow(name, flags=cv2.WINDOW_GUI_NORMAL + cv2.WINDOW_AUTOSIZE)
cv2.waitKey(1)
if NfeCam > 0:
print("[INFO] setting up FishEye camera PTZ windows ...")
for i in range(NfeCam):
name=str("Live_" + CamName[i+FishEyeOffset])
cv2.namedWindow(name, flags=cv2.WINDOW_GUI_NORMAL + cv2.WINDOW_AUTOSIZE)
cv2.waitKey(1)
if Nmqtt > 0:
print("[INFO] setting up MQTT camera image windows ...")
for i in range(Nmqtt):
name=str("Live_" + CamName[i+mqttCamOffset])
cv2.namedWindow(name, flags=cv2.WINDOW_GUI_NORMAL + cv2.WINDOW_AUTOSIZE)
cv2.imshow(name, img)
cv2.waitKey(1)
# setup yolov4 verification windows
if yolo4_verify or yolo8_verify:
print("[INFO] setting up YOLO verification/reject image windows ...")
cv2.namedWindow("yolo_verify", flags=cv2.WINDOW_GUI_NORMAL + cv2.WINDOW_AUTOSIZE)
cv2.imshow("yolo_verify", img)
cv2.waitKey(1)
cv2.namedWindow("yolo_reject",flags=cv2.WINDOW_GUI_NORMAL + cv2.WINDOW_AUTOSIZE)
cv2.imshow("yolo_reject", img)
cv2.waitKey(1)
else:
print("[INFO] setting detection zoom image window ...")
cv2.namedWindow("detection_zoom", flags=cv2.WINDOW_GUI_NORMAL + cv2.WINDOW_AUTOSIZE)
cv2.imshow("detection_zoom", img)
cv2.waitKey(1)
# *** move windows into tiled grid
top=20
left=2
Xborder=3 ## attempt to compensate for openCV window "decorations" varies too much with system to really work
Yborder=32
Xshift=imwinWidth+Xborder
Yshift=imwinHeight+Yborder
Nrows=int(displayHeight/Yshift)
for i in range(Ncameras):
name=str("Live_" + str(i))
col=int(i/Nrows)
row=i%Nrows
cv2.moveWindow(name, left+col*Xshift, top+row*Yshift)
else:
if show_zoom:
print("[INFO] setting detection zoom image window ...")
cv2.namedWindow("detection_zoom", flags=cv2.WINDOW_GUI_NORMAL + cv2.WINDOW_AUTOSIZE)
cv2.imshow("detection_zoom", img)
cv2.waitKey(1)
# *** connect to MQTT broker for control/status messages
print("[INFO] connecting to MQTT " + MQTTserver + " broker...")
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.on_disconnect = on_disconnect
client.will_set("AI/Status", "Python AI has died!", 2, True) # let everyone know we have died, perhaps node-red can restart it
client.connect(MQTTserver, 1883, 60)
client.loop_start()
# starting AI threads can take a long time, send image and message to dasboard to indicate progress
img = np.zeros(( imwinHeight, imwinWidth, 3), np.uint8)
img[:,:] = (192,127,127)
retv, img_as_jpg = cv2.imencode('.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
client.publish("ImageBuffer/!Starting AI threads, this can take awhile!.", bytearray(img_as_jpg), 0, False)
if nCoral + nCPUthreads + nNCSthreads == 0 and not (yolo4AI or yolo8AI):
print("[INFO] No Coral TPU, OpenVINO CPU or GPU devices specified, forcing one CPU AI thread.")
nCPUthreads=1 # we always can force one CPU thread, but ~1.8 seconds/frame on Pi3B+
# these need to be loaded before an AI thread launches them
if yolo4_verify:
#import yolo4 darknet
import darknet_Thread
darknet_Thread.__verifyConf__ = yoloVerifyConf
if yolo8_verify:
#import Ultralytics yolo8 darknet
import yolo8_verification_Thread
# using yolov8x.pt for now m is "fastest" x is "most accurate" l is in between
yolo8_verification_Thread.__y8modelSTR__ = 'yolo8/yolov8x.pt'
yolo8_verification_Thread.__verifyConf__ = yoloVerifyConf
if OVyolo4_verify:
import yolo4OpenvinoVerification_Thread
yolo4OpenvinoVerification_Thread.__verifyConf__ = yoloVerifyConf
if OVmyriad:
yolo4OpenvinoVerification_Thread.__device__ = "MYRIAD"
# *** setup and start Coral AI threads
# Might consider moving this into the thread function.
### Setup Coral AI
# initialize the labels dictionary
if nCoral > 0:
import Coral_TPU_Thread
#$$$# import Prototype_AI_Thread # TPU version of prototype thread for testing/debug
print("[INFO] parsing mobilenet_ssd_v2 coco class labels for Coral TPU...")
modelPath = "mobilenet_ssd_v2/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite"
if Coral_TPU_Thread.__PYCORAL__ == 0:
#$$$# if Prototype_AI_Thread.__PYCORAL__ is False:
labels = {}
for row in open("mobilenet_ssd_v2/coco_labels.txt"):
# unpack the row and update the labels dictionary
(classID, label) = row.strip().split(maxsplit=1)
labels[int(classID)] = label.strip()
print("[INFO] loading Coral mobilenet_ssd_v2_coco model...")
model = Coral_TPU_Thread.DetectionEngine(modelPath)
#$$$#model = Prototype_AI_Thread.DetectionEngine(modelPath)
else:
labels = Coral_TPU_Thread.read_label_file("mobilenet_ssd_v2/coco_labels.txt")
model = Coral_TPU_Thread.make_interpreter(modelPath) # if both installed can't predict which will be used.
##model = Coral_TPU_Thread.make_interpreter(modelPath, "usb") # choose usb TPU if both installed
##model = Coral_TPU_Thread.make_interpreter(modelPath, "pci") # use pci TPU if both installed
#$$$# labels = Prototype_AI_Thread.read_label_file("mobilenet_ssd_v2/coco_labels.txt")
#$$$# model = Prototype_AI_Thread.make_interpreter(modelPath) # if both installed can't predict which will be used.
model.allocate_tensors()
# *** start Coral TPU threads
Ct = list() ## not necessary only supporting a single TPU for now.
print("[INFO] starting " + str(nCoral) + " Coral TPU AI Threads ...")
for i in range(nCoral):
print("... loading model...")
#$$$# Ct.append(Thread(target=Prototype_AI_Thread.AI_thread,
if __DEBUG__:
Coral_TPU_Thread.__DEBUG__ = True
if yolo8_verify:
Coral_TPU_Thread.__VERIFY_DIMS__ = (640,640)
if yolo4_verify or OVyolo4_verify:
Coral_TPU_Thread.__VERIFY_DIMS__ = (608,608)
Ct.append(Thread(target=Coral_TPU_Thread.AI_thread,
args=(results, inframe, model, i, cameraLock, nextCamera, Ncameras,
PREPROCESS_DIMS, confidence, verifyConf, "TPU", blobThreshold, yoloQ)))
Ct[i].start()
# *** setup and start Myriad OpenVINO
## Hmmm... single NCS, Caffe SSDv1 ~9.7 fps with 5 Onvif cameras, TensorFlow SSDv2 gets only ~5.7 fps, with NCS2 ~11.8 fps
## NCS support removed from OpenVINO 2021.1
if nNCSthreads > 0:
if cv2.__version__.find("openvino") > 0:
import OpenVINO_Thread
if SSDv1:
print("[INFO] loading Caffe Mobilenet-SSD model for OpenVINO Myriad NCS2 AI threads...")
OVstr = "CaffeSSDncs"
else:
## fragile works for 2021.1, need better way to detect openVINO version lacks NCS support and needs IR10 models
if cv2.__version__ == "4.5.0-openvino" or cv2.__version__ == "4.5.1-openvino" or cv2.__version__ == "4.5.2-openvino":
print("[INFO] loading Tensor Flow Mobilenet-SSD v2 FP16 IR10 model for OpenVINO_2021.1 Myriad NCS2 AI threads...")
OVstr = "SSDv2_IR10ncs"
else:
print("[INFO] loading Tensor Flow Mobilenet-SSD v2 FP16 model for OpenVINO Myriad NCS2 AI threads...")
OVstr = "SSDv2ncs"
netOV=list()
for i in range(nNCSthreads):
print("... loading model...")
if SSDv1:
netOV.append(cv2.dnn.readNetFromCaffe("MobileNetSSD/MobileNetSSD_deploy.prototxt", "MobileNetSSD/MobileNetSSD_deploy.caffemodel"))
OpenVINO_Thread.SSDv1 = True
else:
## fragile works for 2021.1, need better way to detect openVINO version lacks NCS support and needs IR10 models
if cv2.__version__ == "4.5.0-openvino" or cv2.__version__ == "4.5.1-openvino" or cv2.__version__ == "4.5.2-openvino":
netOV.append(cv2.dnn.readNet("mobilenet_ssd_v2/MobilenetSSDv2cocoIR10.xml", "mobilenet_ssd_v2/MobilenetSSDv2cocoIR10.bin"))
else:
netOV.append(cv2.dnn.readNet("mobilenet_ssd_v2/MobilenetSSDv2coco.xml", "mobilenet_ssd_v2/MobilenetSSDv2coco.bin"))
netOV[i].setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
netOV[i].setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD) # specify the target device as the Myriad processor on the NCS
# *** start OpenVINO AI threads
OVt = list()
print("[INFO] starting " + str(nNCSthreads) + " OpenVINO Myriad NCS2 AI Threads ...")
if yolo8_verify:
OpenVINO_Thread.__VERIFY_DIMS__ = (640,640)
if yolo4_verify or OVyolo4_verify:
OpenVINO_Thread.__VERIFY_DIMS__ = (608,608)
for i in range(nNCSthreads):
OVt.append(Thread(target=OpenVINO_Thread.AI_thread,
args=(results, inframe, netOV[i], i, cameraLock, nextCamera, Ncameras,
PREPROCESS_DIMS, confidence-0.1, verifyConf-0.1, OVstr, blobThreshold, yoloQ))) # these seem less sensitive
OVt[i].start()
else:
print("[ERROR!] OpenVINO version of openCV is not active, check $PYTHONPATH")
print(" No MYRIAD (NCS/NCS2) OpenVINO threads will be created!")
nNCSthreads = 0
if nCoral+nCPUthreads == 0:
print("[INFO] No Coral TPU device or CPU threads specified, forcing one CPU AI thread.")
nCPUthreads=1 # we always can force one CPU thread, but ~1.8 seconds/frame on Pi3B+
# ** setup and start CPU/GPU AI threads, usually only one makes sense.
## TODO: do I want SSDv2 option for CPU threads as well?? Done, Made FP32 version with Model Optimizer.
## Will need to make FP32 version, SSDv2 error: "Inference Engine backend: The plugin does not support FP16 in function 'initPlugin'"
if nCPUthreads > 0:
net=list()
if cv2.__version__.find("openvino") > 0:
import OpenVINO_Thread
if SSDv1:
print("[INFO] loading Caffe Mobilenet-SSD model for OpenVINO CPU AI threads...")
OVstr = "SSDv1_cpu"
OpenVINO_Thread.SSDv1 = True
for i in range(nCPUthreads):
net.append(cv2.dnn.readNetFromCaffe("MobileNetSSD/MobileNetSSD_deploy.prototxt", "MobileNetSSD/MobileNetSSD_deploy.caffemodel"))
net[i].setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
net[i].setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
else:
OpenVINO_Thread.SSDv1 = False
if cv2.__version__ == "4.5.0-openvino" or cv2.__version__ == "4.5.1-openvino" or cv2.__version__ == "4.5.2-openvino":
print("[INFO] loading Tensor Flow Mobilenet-SSD v2 FP16 IR10 model for OpenVINO_2021.1...")
if useGPU:
OVstr = "SSDv2_IR10gpu"
else:
OVstr = "SSDv2_IR10cpu"
else:
print("[INFO] loading Tensor Flow Mobilenet-SSD v2 FP32 model for OpenVINO CPU AI threads...")
OVstr = "SSDv2_FP32cpu"
for i in range(nCPUthreads):
if cv2.__version__ == "4.5.0-openvino" or cv2.__version__ == "4.5.1-openvino" or cv2.__version__ == "4.5.2-openvino":
net.append(cv2.dnn.readNet("mobilenet_ssd_v2/MobilenetSSDv2cocoIR10.xml", "mobilenet_ssd_v2/MobilenetSSDv2cocoIR10.bin"))
net[i].setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)