add differing methods for opencv ver 3/4

This commit is contained in:
Lennart Heimbs 2019-08-04 12:05:22 +02:00
parent 4426205b0f
commit a26557345f
2 changed files with 19 additions and 37 deletions

View File

@ -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 -i PATH_TO_IMAGE # Reads and detect people in a single local stored image
python peopleCounter.py -c # Attempts to detect people using webcam 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 = cv2.HOGDescriptor()
HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
@ -56,26 +44,6 @@ def buildPayload(variable, value, context):
return {variable: {"value": value, "context": 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(): def argsParser():
ap = argparse.ArgumentParser() ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", default=None, 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])) image = imutils.resize(image, width=min(400, image.shape[1]))
clone = image.copy() clone = image.copy()
if len(image) <= 0: if len(image) <= 0:
print("[ERROR] could not read your local image") print("[ERROR] could not read local image")
return result return result
print("[INFO] Detecting people") print("[INFO] Detecting people")
result = detector(image) result = detector(image)
# shows the result """# shows the result
for (xA, yA, xB, yB) in result: for (xA, yA, xB, yB) in result:
cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
@ -106,8 +74,8 @@ def localDetect(image_path):
cv2.waitKey(0) cv2.waitKey(0)
cv2.destroyAllWindows() cv2.destroyAllWindows()
cv2.imwrite("result.png", np.hstack((clone, image))) cv2.imwrite("result.png", np.hstack((clone, image)))"""
return (result, image) return result#(result, image)
def cameraDetect(token, device, variable, sample_time=5): def cameraDetect(token, device, variable, sample_time=5):

View File

@ -20,6 +20,12 @@ args = vars(ap.parse_args())
""" Determine opencv version and select tracker """ """ Determine opencv version and select tracker """
# extract the OpenCV version info # extract the OpenCV version info
(major, minor) = cv2.__version__.split(".")[:2] (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 # 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 # function to create the entity that tracks objects
if int(major) == 3 and int(minor) < 3: if int(major) == 3 and int(minor) < 3:
@ -62,8 +68,13 @@ now = ''
framecounter = 0 framecounter = 0
trackeron = 0 trackeron = 0
people_count_total = 0 people_count_total = 0
frame_counter= 0
while True: while True:
"""frame_counter+=1
if framecounter%5 != 0:
continue"""
people_count_per_frame = 0 people_count_per_frame = 0
frame = vs.read() frame = vs.read()
frame = frame if args.get("video", None) is None else frame[1] 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 # dilate the thresholded image to fill in holes, then find contours on thresholded image
thresh = cv2.dilate(thresh, None, iterations=2) thresh = cv2.dilate(thresh, None, iterations=2)
thresh = np.uint8(thresh) 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 #cnts = cnts if imutils.is_cv2() else im2
#print(len(cnts)) #print(len(cnts))
#if len(cnts) > 1: #if len(cnts) > 1: