From a26557345f4d83fb856a9ef1e8da85763dff180d Mon Sep 17 00:00:00 2001 From: Lennart Heimbs Date: Sun, 4 Aug 2019 12:05:22 +0200 Subject: [PATCH] add differing methods for opencv ver 3/4 --- camera/counter_people.py | 40 ++++------------------------------------ camera/video_presence.py | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/camera/counter_people.py b/camera/counter_people.py index 37accc2..5bdd166 100644 --- a/camera/counter_people.py +++ b/camera/counter_people.py @@ -13,20 +13,8 @@ Usage: python peopleCounter.py -i PATH_TO_IMAGE # Reads and detect people in a single local stored image python peopleCounter.py -c # Attempts to detect people using webcam - -IMPORTANT: This example is given AS IT IS without any warranty - -Made by: Jose Garcia - ''' -URL_EDUCATIONAL = "http://things.ubidots.com" -URL_INDUSTRIAL = "http://industrial.api.ubidots.com" -INDUSTRIAL_USER = False # Set this to False if you are an educational user -TOKEN = "A1E-VQSZw6exCjViKRfqHl7ISdrVEm3cG1" # Put here your Ubidots TOKEN -DEVICE = "detector" # Device where will be stored the result -VARIABLE = "people" # Variable where will be stored the result - HOGCV = cv2.HOGDescriptor() HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) @@ -56,26 +44,6 @@ def buildPayload(variable, value, context): return {variable: {"value": value, "context": context}} -def sendToUbidots(token, device, variable, value, context={}, industrial=True): - # Builds the endpoint - url = URL_INDUSTRIAL if industrial else URL_EDUCATIONAL - url = "{}/api/v1.6/devices/{}".format(url, device) - - payload = buildPayload(variable, value, context) - headers = {"X-Auth-Token": token, "Content-Type": "application/json"} - - attempts = 0 - status = 400 - - while status >= 400 and attempts <= 5: - req = requests.post(url=url, headers=headers, json=payload) - status = req.status_code - attempts += 1 - time.sleep(1) - - return req - - def argsParser(): ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", default=None, @@ -93,12 +61,12 @@ def localDetect(image_path): image = imutils.resize(image, width=min(400, image.shape[1])) clone = image.copy() if len(image) <= 0: - print("[ERROR] could not read your local image") + print("[ERROR] could not read local image") return result print("[INFO] Detecting people") result = detector(image) - # shows the result + """# shows the result for (xA, yA, xB, yB) in result: cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) @@ -106,8 +74,8 @@ def localDetect(image_path): cv2.waitKey(0) cv2.destroyAllWindows() - cv2.imwrite("result.png", np.hstack((clone, image))) - return (result, image) + cv2.imwrite("result.png", np.hstack((clone, image)))""" + return result#(result, image) def cameraDetect(token, device, variable, sample_time=5): diff --git a/camera/video_presence.py b/camera/video_presence.py index 4b08cef..af5ee24 100755 --- a/camera/video_presence.py +++ b/camera/video_presence.py @@ -20,6 +20,12 @@ args = vars(ap.parse_args()) """ Determine opencv version and select tracker """ # extract the OpenCV version info (major, minor) = cv2.__version__.split(".")[:2] +# different methods of opencv require differing ways to unpack find countours +if int(major) > 3: + OPENCV4=True +else: + OPENCV4=False + # if we are using OpenCV 3.2 or an earlier version, we can use a special factory # function to create the entity that tracks objects if int(major) == 3 and int(minor) < 3: @@ -62,8 +68,13 @@ now = '' framecounter = 0 trackeron = 0 people_count_total = 0 +frame_counter= 0 while True: + """frame_counter+=1 + if framecounter%5 != 0: + continue""" + people_count_per_frame = 0 frame = vs.read() frame = frame if args.get("video", None) is None else frame[1] @@ -93,7 +104,10 @@ while True: # dilate the thresholded image to fill in holes, then find contours on thresholded image thresh = cv2.dilate(thresh, None, iterations=2) thresh = np.uint8(thresh) - _, cnts, im2 = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) + if OPENCV4: + cnts, im2 = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) + else: + _, cnts, im2 = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) #cnts = cnts if imutils.is_cv2() else im2 #print(len(cnts)) #if len(cnts) > 1: