Python: Multiple Process Running Issue OPENCV & Raspberry Pi -
here's source code playing on raspberry pi 3+ .
#!/usr/bin/env python import cv2 import numpy np import time import count_helper imutils.video import videostream sort import * import argparse import configparser import mysqldb import datetime # creating connection db = mysqldb.connect(host="localhost", # host, localhost user="root", # username passwd="root", # password db="peoplecount") # name of data base # creating cursor cur = db.cursor() config = configparser.rawconfigparser() config.read('config.cfg') # full url url_full = '{}/api/peoplecount/'.format(config.get('server', 'url')) parser = argparse.argumentparser() parser.add_argument("--video", type=str) parser.add_argument("--display", type=str) parser.add_argument("--numline", type=str, default="single") args = parser.parse_args() starttime = time.time() mot_tracker = sort() if args.video == "picam": c = videostream(usepicamera=1, resolution=(640, 480)).start() time.sleep(2.0) f = c.read() f = cv2.resize(f, (400, 300)) elif args.video == "cam": c = cv2.videocapture(0) ret, f = c.read() f = cv2.resize(f, (400, 300)) else: c = cv2.videocapture(args.video) ret, f = c.read() f = cv2.resize(f, (400, 300)) width = int(f.shape[1]) height = int(f.shape[0]) bgs_mog = cv2.createbackgroundsubtractormog2() kernelop = np.ones((3, 3), np.uint8) kernelcl = np.ones((11, 11), np.uint8) y1 = config.getint('line', 'y1') y2 = config.getint('line', 'y2') entry_disp = 0 exit_disp = 0 while true: s = time.time() if args.video == "picam": f = c.read() else: ret, f = c.read() if not ret: c.open(args.video) time.sleep(0.5) continue f = cv2.resize(f, (400, 300)) s = time.time() grey_image = bgs_mog.apply(f) thresh, im_bw = cv2.threshold(grey_image, 225, 255, cv2.thresh_binary) im_dl = cv2.morphologyex(im_bw, cv2.morph_open, kernelop) im_dl = cv2.morphologyex(im_dl, cv2.morph_close, kernelcl) im_dl, contours, hierarchy = cv2.findcontours(im_dl, cv2.retr_external, cv2.chain_approx_simple) bboxes = [] centroids = [] cnt in contours: try: x, y, w, h = cv2.boundingrect(cnt) if args.display.lower() == 'true': cv2.rectangle(f, (x, y), (x + w, y + h), (255, 0, 0), 2) bboxes.append([x, y, x + w, y + h, 0.9]) except: pass bboxes = np.asarray(bboxes) track_bbs_ids = mot_tracker.update(bboxes) if args.display.lower() == 'true': if args.numline.lower() == 'double': cv2.line(f, (0, y1), (400, y1), (0, 0, 255), 2) cv2.line(f, (0, y2), (400, y2), (255, 0, 0), 2) else: cv2.line(f, (0, y1), (400, y1), (0, 0, 255), 2) ids in track_bbs_ids: cv2.puttext(f, str(int(ids[4])), (int(ids[2]), int(ids[1])), cv2.font_hershey_simplex, 1, (255, 255, 255), 1) if args.numline.lower() == 'double': exit, entry = count_helper.counting_doubleline(y1, y2, track_bbs_ids) else: exit, entry = count_helper.counting_singleline(y1, track_bbs_ids) if entry > 0 or exit > 0: # write db here camera_id = config.get('server', 'camera') store_id = config.get('server', 'store') profile_id = config.get('server', 'profile') recorded_time = datetime.datetime.now().strftime('%y-%m-%d %h:%m:%s') query = "insert pc(timestamp, people_count_entry, people_count_exit, status, store_id, profile_id, camera_id) values('%s', %d, %d, %d, %s, %s, %s)" % (recorded_time, entry, exit, 0, store_id, profile_id, camera_id) # noqa try: cur.execute(query) db.commit() except: print "check entry" entry_disp += entry exit_disp += exit if args.display.lower() == 'true': cv2.puttext(f, 'entry:' + str(entry_disp) + ', exit:' + str(exit_disp), (20, 20), cv2.font_hershey_simplex, 1, (255, 255, 255), 2) cv2.imshow('output', f) k = cv2.waitkey(1) if k == 27: break cv2.destroyallwindows() c.release() enter code here
i have named file counting-sort.py , registered service in /etc/systemd/system/detection.service.
problem when do
sudo service detection start
can see multiple instance of script being evoked. output when htop
on pi machine. htop output can me why happening? due internal threading of opencv bindings python?
thanks in advance.
edit :
here's pstree -a
output :
```
├─python counting-sort.py --display false --video picam │ ├─{hcec notify} │ ├─{hdispmanx notif} │ ├─{htv notify} │ ├─{vchiq completio} │ ├─14*[{python}] │ ├─{vc.null_sink} │ ├─{vc.ril.camera} │ └─{vc.ril.video_sp} ```
Comments
Post a Comment