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.

person_detection.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env python
  2. from imutils.video import VideoStream
  3. from imutils.video import FPS
  4. import argparse
  5. import imutils
  6. import cv2
  7. from datetime import datetime, time
  8. import numpy as np
  9. import time as time2
  10. VISUAL_DEBUG=True
  11. def getArgs():
  12. """ Arguments """
  13. ap = argparse.ArgumentParser()
  14. ap.add_argument("-v", "--video", help="path to the video file")
  15. ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
  16. ap.add_argument("-t", "--tracker", type=str, default="csrt", help="OpenCV object tracker type")
  17. return vars(ap.parse_args())
  18. def main():
  19. args = getArgs()
  20. # if the video argument is None, then the code will read from webcam (work in progress)
  21. if args.get("video", None) is None:
  22. vs = VideoStream(src=0).start()
  23. time2.sleep(2.0)
  24. # otherwise, we are reading from a video file
  25. else:
  26. vs = cv2.VideoCapture(args["video"])
  27. cv2.namedWindow('Video stream', cv2.WINDOW_NORMAL)
  28. detector = DetectionFromFrame(args["min_area"], 0.8)
  29. while True:
  30. detector.currentFrame = vs.read()
  31. detector.currentFrame = detector.currentFrame if args.get("video", None) is None else detector.currentFrame[1]
  32. # if the frame can not be grabbed, then we have reached the end of the video
  33. if detector.currentFrame is None:
  34. break
  35. # resize the frame to 500
  36. detector.currentFrame = imutils.resize(detector.currentFrame, width=500)
  37. detector.framecounter+=1
  38. if detector.framecounter > 1:
  39. cnts = detector.prepareFrame()
  40. for c in cnts:
  41. boundRect = cv2.boundingRect(c)
  42. #(x, y, w, h) = cv2.boundingRect(c)
  43. #initBB2 =(x,y,w,h)
  44. prott1 = r'ML-Models/MobileNetSSD_deploy.prototxt'
  45. prott2 = r'ML-Models/MobileNetSSD_deploy.caffemodel'
  46. net = cv2.dnn.readNetFromCaffe(prott1, prott2)
  47. #trackbox = detector.currentFrame[y:y+h, x:x+w]boundRect[1]
  48. trackbox = detector.currentFrame[boundRect[1]:boundRect[1]+boundRect[3],
  49. boundRect[0]:boundRect[0]+boundRect[2]]
  50. trackbox = cv2.resize(trackbox, (224, 224))
  51. #cv2.imshow('image',trackbox)
  52. blob = cv2.dnn.blobFromImage(cv2.resize(trackbox, (300, 300)),0.007843, (300, 300), 127.5)
  53. net.setInput(blob)
  54. detections = net.forward()
  55. for i in np.arange(0, detections.shape[2]):
  56. detector.detectConfidentiallyPeople(i, detections, boundRect)
  57. cv2.rectangle(detector.currentFrame, (boundRect[0], boundRect[1]),
  58. (boundRect[0] + boundRect[2], boundRect[1] + boundRect[3]), (255, 255, 0), 1)
  59. # show the frame and record if the user presses a key
  60. cv2.imshow("Video stream", detector.currentFrame)
  61. key = cv2.waitKey(1) & 0xFF
  62. # if the `q` key is pressed, break from the lop
  63. if key == ord("q"):
  64. break
  65. if key == ord("d"):
  66. detector.firstFrame = None
  67. #detector.lastFrame = detector.currentFrame
  68. # finally, stop the camera/stream and close any open windows
  69. vs.stop() if args.get("video", None) is None else vs.release()
  70. cv2.destroyAllWindows()
  71. class DetectionFromFrame:
  72. def __init__(self, min_size, confidence):
  73. self.min_size = min_size
  74. self.confidence_level = confidence
  75. self.firstFrame = None
  76. self.currentFrame = None
  77. self.initBB2 = None
  78. self.fps = None
  79. self.differ = None
  80. self.now = ''
  81. self.framecounter = 0
  82. self.people_count_total = 0
  83. def prepareFrame(self):
  84. gray = cv2.cvtColor(self.currentFrame, cv2.COLOR_BGR2GRAY)
  85. gray = cv2.GaussianBlur(gray, (21, 21), 0)
  86. # if the first frame is None, initialize it
  87. if self.firstFrame is None:
  88. self.firstFrame = gray
  89. return []
  90. # compute the absolute difference between the current frame and first frame
  91. frameDelta = cv2.absdiff(self.firstFrame, gray)
  92. thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
  93. #debug
  94. """if VISUAL_DEBUG:
  95. cv2.imshow("debug image", thresh)
  96. cv2.waitKey(0)
  97. cv2.destroyWindow("debug image")
  98. #cv2.destroyWindow("threshhold image")"""
  99. # dilate the thresholded image to fill in holes
  100. thresh = cv2.dilate(thresh, None, iterations=2)
  101. # find contours on thresholded image
  102. thresh = np.uint8(thresh)
  103. cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  104. return cnts
  105. def detectConfidentiallyPeople(self, i, detections, boundRect):
  106. CLASSES = ["person"]
  107. COLORS = [0,255,0]
  108. #COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
  109. confidence = detections[0, 0, i, 2]
  110. if confidence > self.confidence_level:
  111. # extract the index of the class label from the `detections`, then compute the (x, y)-coordinates of
  112. # the bounding box for the object
  113. #idx = int(detections[0, 0, i, 1])
  114. box = detections[0, 0, i, 3:7] * np.array([boundRect[2], boundRect[3], boundRect[2], boundRect[3]])
  115. (startX, startY, endX, endY) = box.astype("int")
  116. # draw the prediction on the frame
  117. #label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)
  118. label = "{}: {:.2f}%".format(CLASSES[0], confidence * 100)
  119. #cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2)
  120. cv2.rectangle(self.currentFrame, (boundRect[0], boundRect[1]),
  121. (boundRect[0] + boundRect[2], boundRect[1] + boundRect[3]), (0,255, 0), 3)
  122. y = boundRect[1] - 15 if boundRect[1] - 15 > 15 else boundRect[1] + 15
  123. #cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
  124. cv2.putText(self.currentFrame, label, (0, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1)
  125. #cv2.imshow("Video stream", self.currentFrame)
  126. #print("Person found")
  127. if __name__ == "__main__":
  128. main()