Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

counter_people.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from imutils.object_detection import non_max_suppression
  2. import numpy as np
  3. import imutils
  4. import cv2
  5. import time
  6. import argparse
  7. import time
  8. import base64
  9. '''
  10. Usage:
  11. python peopleCounter.py -i PATH_TO_IMAGE # Reads and detect people in a single local stored image
  12. python peopleCounter.py -c # Attempts to detect people using webcam
  13. '''
  14. HOGCV = cv2.HOGDescriptor()
  15. HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
  16. def detector(image):
  17. '''
  18. @image is a numpy array
  19. '''
  20. clone = image.copy()
  21. (rects, weights) = HOGCV.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05)
  22. # draw the original bounding boxes
  23. for (x, y, w, h) in rects:
  24. cv2.rectangle(clone, (x, y), (x + w, y + h), (0, 0, 255), 2)
  25. # Applies non-max supression from imutils package to kick-off overlapped
  26. # boxes
  27. rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
  28. result = non_max_suppression(rects, probs=None, overlapThresh=0.65)
  29. return result
  30. def buildPayload(variable, value, context):
  31. return {variable: {"value": value, "context": context}}
  32. def argsParser():
  33. ap = argparse.ArgumentParser()
  34. ap.add_argument("-i", "--image", default=None, help="path to image test file directory")
  35. ap.add_argument("-c", "--camera", default=False, help="Set as true if you wish to use the camera")
  36. ap.add_argument("-v", "--video", default=None, help="path to the video file")
  37. args = vars(ap.parse_args())
  38. return args
  39. def localDetect(image_path):
  40. result = []
  41. image = cv2.imread(image_path)
  42. image = imutils.resize(image, width=min(400, image.shape[1]))
  43. clone = image.copy()
  44. if len(image) <= 0:
  45. print("[ERROR] could not read local image")
  46. return result
  47. print("[INFO] Detecting people")
  48. result = detector(image)
  49. """# shows the result
  50. for (xA, yA, xB, yB) in result:
  51. cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
  52. cv2.imshow("result", image)
  53. cv2.waitKey(0)
  54. cv2.destroyAllWindows()
  55. cv2.imwrite("result.png", np.hstack((clone, image)))"""
  56. return result#(result, image)
  57. def cameraDetect(video_path="", sample_time=5):
  58. if video_path:
  59. cap = cv2.VideoCapture(video_path)
  60. else:
  61. cap = cv2.VideoCapture(0)
  62. #init = time.time()
  63. while(True):
  64. # Capture frame-by-frame
  65. _, frame = cap.read()
  66. if frame is None:
  67. break
  68. frame = imutils.resize(frame, width=min(400, frame.shape[1]))
  69. result = detector(frame.copy())
  70. # shows the result
  71. for (xA, yA, xB, yB) in result:
  72. cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)
  73. cv2.imshow('frame', frame)
  74. cv2.waitKey(0)
  75. #if time.time() - init >= sample_time:
  76. if len(result):
  77. print("{} people detected.".format(len(result)))
  78. #init = time.time()
  79. if cv2.waitKey(1) & 0xFF == ord('q'):
  80. break
  81. # When everything done, release the capture
  82. cap.release()
  83. cv2.destroyAllWindows()
  84. def convert_to_base64(image):
  85. image = imutils.resize(image, width=400)
  86. img_str = cv2.imencode('.png', image)[1].tostring()
  87. b64 = base64.b64encode(img_str)
  88. return b64.decode('utf-8')
  89. def detectPeople(args):
  90. image_path = args["image"]
  91. video_path = args["video"]
  92. camera = True if str(args["camera"]) == 'true' else False
  93. # Routine to read local image
  94. if image_path != None and not camera and video_path == None:
  95. print("[INFO] Image path provided, attempting to read image")
  96. (result, image) = localDetect(image_path)
  97. print(str(len(result)) + " People detected.")
  98. if video_path != None and not camera:
  99. print("[INFO] reading video")
  100. cameraDetect(video_path)
  101. # Routine to read images from webcam
  102. if camera:
  103. print("[INFO] reading camera images")
  104. cameraDetect()
  105. def main():
  106. args = argsParser()
  107. detectPeople(args)
  108. if __name__ == '__main__':
  109. main()