arg_parse.add_argument("-i", "--image", default=None, help="path to Image File ") arg_parse.add_argument("-c", "--camera", default=False, help="Set true if you want to use the camera.") arg_parse.add_argument("-o", "--output", type=str, help="path to optional output video file") args = vars(arg_parse.parse_args()) return args if __name__ == "__main__": HOGCV = cv2.HOGDescriptor() HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) args = argsParser() humanDetector(args) # to run, type the following in terminal # to detect people in a video: python main.py -v ‘Path_to_video’ # to detect people in an image: python main.py -i ‘Path_to-image’ # for example: python main.py -i 002.jpg # to use camera: python main.py -c True # to save the output: Python main.py -c True -o ‘file_name’
def track(x, y, w, h, f): trackerVar = 0 varia = str(trackerVar) #creates tracker varia = cv2.Tracker_create("KCF") x = x y = y #creates bounding box xC = (int)(x - 1.5 * w) yC = (int)(y - 0.75 * w) height = h * 5 width = (int)(w * 3.5) c, r, w, h = xC, yC, width, height bbox = (c, r, w, h) #intialized tracker with the created bounding box ok = varia.init(f, bbox) ratioX, ratioY = initDist(f, width, height) num = 0 #once we get to looking for distance every 5 frames var = 0 turn = 0 forward = 0 nonLocate = 0 while True: msg = rospy.wait_for_message("/ardrone/image_raw", imageX) frame = ros_numpy.numpify(msg) ok = True #updates position of bounding box using the KCF tracker ok, bbox = varia.update(frame) if ok: p1 = (int(bbox[0]), int(bbox[1])) p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) #draws bounding box on the current frame cv2.rectangle(frame, p1, p2, (0, 0, 255)) if (num == 3): #check initDistance for descr of pedestrian detector position = 0 hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) image = imutils.resize(frame, width=min(400, frame.shape[1])) orig = image.copy() (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05) for (x, y, w, h) in rects: cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2) rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) par = 0 for (xA, yA, xB, yB) in pick: par += 1 cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) if (par > 0): #checks if bounding box is close enough using pedestrian detector if (abs(((xB + xA) / 2) - (p1[0] + p2[0]) / 2) > 120 or abs(((yB + yA) / 2) - ((p1[1] + p2[1]) / 2)) > 40): cv2.circle(image, (((xB + xA) / 2), ((yB + yA) / 2)), 2, (0, 0, 255), 3) cv2.circle(image, (p1[0], p1[1]), 2, (0, 255, 0), 3) print "fixing bbox" xC = xB yC = yA #height=h*5 #width= (int) (w*3.5) #creates new bounding box and creates/initializes a new tracker c, r, w, h = xC + width / 2, yC + height / 2, width, height bbox = (c, r, w, h) trackerVar += 1 varia = str(trackerVar) varia = cv2.Tracker_create("KCF") ok = varia.init(frame, bbox) #sets whether it should be moving forward or backward or neither if ((float)(yB - yA) / ratioY > 1.3): position = 2 print(float)(yB - yA) / ratioY elif ((float)(yB - yA) / ratioY < 0.95): position = 1 else: position = 0 else: print "couldnt find person" #logic for setting commands for drone- could be accomplished much easier with global variables # but not sure if possible with current framework if (turn == 0): SendCommand(0, 0, 0, 0) rospy.sleep(0.3) forward = 0 elif (turn == 1): SendCommand(0, 0, 0.3, 0) forward = 0 elif (turn == -1): SendCommand(0, 0, -0.3, 0) forward = 0 if (nonLocate > 5): SendCommand(0, 0, 0, 0) nonLocate = 0 else: nonLocate += 1 if (position == 1): if (turn == 0): SendCommand(0, 0.1, 0, 0) forward = 1 elif (turn == 1): SendCommand(0, 0.1, 0.3, 0) forward = 1 elif (turn == -1): SendCommand(0, 0.1, -0.3, 0) forward = 1 print "forward" #rospy.sleep(1) elif (position == 2): if (turn == 0): SendCommand(0, -0.1, 0, 0) forward = 2 elif (turn == 1): SendCommand(0, -0.1, 0.3, 0) forward = 2 elif (turn == -1): SendCommand(0, -0.1, -0.3, 0) forward = 2 print "backward" else: print "nuetral" if (turn == 0): SendCommand(0, 0, 0, 0) forward = 0 elif (turn == 1): SendCommand(0, 0, 0.3, 0) forward = 0 elif (turn == -1): SendCommand(0, 0, -0.3, 0) forward = 0 num = 0 else: num += 1 #displays current frame with bounding box drawn on cv2.imshow("Tracking", frame) #finds angle of center of bbox angle = findAngle((p1[0] + p2[0]) / 2) #checks angle and logic for setting drone commands if (angle <= 5 and angle >= -5 and var > 0): if (forward == 0): SendCommand(0, 0, 0, 0) elif (forward == 1): SendCommand(0, 0.1, 0, 0) elif (forward == -1): SendCommand(0, -0.1, 0, 0) turn = 0 var = 0 elif (angle > 5 and var == 0): if (forward == 0): SendCommand(0, 0, -0.3, 0) elif (forward == 1): SendCommand(0, 0.1, -0.3, 0) elif (forward == -1): SendCommand(0, -0.1, -0.3, 0) turn = -1 var += 1 elif (angle < -5 and var == 0): if (forward == 0): SendCommand(0, 0, 0.3, 0) elif (forward == 1): SendCommand(0, 0.1, 0.3, 0) elif (forward == -1): SendCommand(0, -0.1, 0.3, 0) turn = 1 var += 1 #update angle k = cv2.waitKey(1) & 0xff if k == 27: break
@author: hasee """ import cv2 import numpy as np import glob import os from sklearn.decomposition import PCA import matplotlib.pyplot as plt from sklearn import svm from sklearn import datasets,decomposition,manifold from mpl_toolkits.mplot3d import Axes3D from sklearn.svm import SVC hog = cv2.HOGDescriptor() YES_PATH = './train/p'#存放脸图片的文件夹路径 NO_PATH = './train/n' paths = glob.glob(os.path.join(YES_PATH, '*.jpg')) paths.sort() nopath = glob.glob(os.path.join(NO_PATH, '*.jpg')) #hoglist = np. b = [] noface = [] def hog_features(image): hog_computer = cv2.HOGDescriptor() return hog_computer.compute(image) for path in paths:
for i in range(len(test_csv)): img = resize_image(load_image("images/test/" + test_csv["file"][i])) test_images.append(img) test_labels.append(test_csv["labels"][i]) print("Loaded test images: ", test_labels) nbins = 20 cell_size = (24, 24) block_size = (6, 6) hog = cv2.HOGDescriptor(_winSize=(img.shape[1] // cell_size[1] * cell_size[1], img.shape[0] // cell_size[0] * cell_size[0]), _blockSize=(block_size[1] * cell_size[1], block_size[0] * cell_size[0]), _blockStride=(cell_size[1], cell_size[0]), _cellSize=(cell_size[1], cell_size[0]), _nbins=nbins) print("Computing, please wait...") features_train = [] for img in train_images: features_train.append(hog.compute(img)) x = reshape_data(np.array(features_train)) y = np.array(train_labels) clf_svm = SVC(kernel='linear') clf_svm = clf_svm.fit(x, y)
import imutils import cv2 ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", help="path to the video file (optional)") args = vars(ap.parse_args()) # Use attached webcam if no arguments. Use video file otherwise. if not args.get("video", False): camera = cv2.VideoCapture(0) else: camera = cv2.VideoCapture(args["video"]) #initialise HOG HOG = cv2.HOGDescriptor() HOG.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) time.sleep(0.1) while True: (grabbed, frame) = camera.read() if args.get("video") and not grabbed: break hogRects, w = HOG.detectMultiScale(frame, winStride=(8, 8), padding=(32, 32), scale=1.05) frameClone = frame.copy() for (fX, fY, fW, fH) in hogRects: cv2.rectangle(frameClone, (fX, fY), (fX+fW, fY+fH), (0, 255, 0), 2)
plabels = np.ones(len(posImages)) nlabels = np.zeros(len(negImages)) winSize = (20, 20) blockSize = (8, 8) blockStride = (4, 4) cellSize = (8, 8) nbins = 9 derivAperture = 1 winSigma = -1 histogramNormType = 0 L2HysThreshold = 0.2 gammaCorrection = 1 nlevels = 64 signedGradient = True hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins, derivAperture, winSigma, histogramNormType, L2HysThreshold, gammaCorrection, nlevels, signedGradient) hog_descriptors_pos = [] for img in posImages: hog_descriptors_pos.append(hog.compute(img)) hog_descriptors_pos = np.squeeze(hog_descriptors_pos) hog_descriptors_neg = [] for img in negImages: hog_descriptors_neg.append(hog.compute(img)) hog_descriptors_neg = np.squeeze(hog_descriptors_neg) train_p = int(0.9 * len(hog_descriptors_pos)) train_n = int(0.9 * len(hog_descriptors_neg)) hog_descriptors_pos_train, hog_descriptors_pos_test = np.split( hog_descriptors_pos, [train_p])
ImageSize = (128, 128) winSize = (128, 128) blockSize = (64, 64) blockStride = (32, 32) cellSize = (16, 16) nbins = 9 # blockSize必须被cellSize整除 # winSize-blockSize必须被blockStride整除 # 维度=nbins*4*(winSize.height/blockStride.height-1)*(winSize.width/blockStride.width-1) # img_hog = img_gray img_hog = cv2.resize(img_mean, ImageSize) hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins) img_hog = hog.compute(img_hog) img_hog = np.squeeze(img_hog, -1) h_mean, h_std = np.squeeze( cv2.meanStdDev(image_H, img_binary_d), -1) # 一阶颜色矩 s_mean, s_std = np.squeeze( cv2.meanStdDev(image_S, img_binary_d), -1) v_mean, v_std = np.squeeze( cv2.meanStdDev(image_V, img_binary_d), -1) # h_std = cv2.std(image_H) # 二阶颜色矩 # s_std = cv2.std(image_S)
self.video_device.set(3, width) self.video_device.set(4, heigth) print(self.video_device.get(3), self.video_device.get(4)) self.video_device.set(10, 100) def __del__(self): self.video_device.release() # 释放video设备 if __name__ == '__main__': source = "rtsp://*****:*****@192.168.1.4/Streaming/Channels/1" vc = Video(source) # vc.set_pixel(1500, 1500) body = cv.CascadeClassifier('../classifier_machine/haar/face.xml') hog = cv.HOGDescriptor() # hog.load('myHogDector.bin') hog.setSVMDetector(cv.HOGDescriptor_getDefaultPeopleDetector()) #0: 120:40 l_set = 1380 t_set = 20 w_set = 400 h_set = 400 while True: time_1 = time.time() fram = vc.video_device.read()[1] cv.rectangle(fram, (l_set, t_set), (l_set + w_set, t_set + h_set), (0, 0, 255), 4)
def fd_hog(image): hog = cv2.HOGDescriptor() img_squished = cv2.resize(image, (64,128)) locations = ((0,0),) h = hog.compute(image, locations = locations) return h.flatten()
HOGDescriptor, SVM은 opencv를 이용하였고 NMS는 imutils을 이용하였습니다. 또한 HOG feature가 어떻게 뽑히는지 시각화하기 위한 결과를 추가하였으며 해당 작업은 opencv로 해보려고 했는데 잘 되지 않아서 skimage를 이용하였습니다. ''' # 이미지 로드 및 복사 image = cv2.imread('test.png') image2 = image.copy() # opencv의 HOGDescriptor + setSVMDetector를 이용하여 HOG feature 추출 및 보행자를 검출합니다. hog_opencv = cv2.HOGDescriptor() hog_opencv.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # window stirde, padding, scale 등의 파라미터 값을 설정하고 바운딩 박스 좌표를 리턴 rects, weight = hog_opencv.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.5) # 검출된 영역을 빨간색 바운딩 박스로 이미지에 그림 for (x, y, w, h) in rects: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 0, 255), 2) # NMS 알고리즘을 사용하여 중복된 box를 걸러냄 rects = np.array([[x, y, x+w, y+h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) # 중복된 box를 걸러낸 후 남은 box를 초록색 박스로 그림
def main(): # initialize the HOG descriptor/person detector camera = cv2.VideoCapture(0) time.sleep(0.25) hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) Threshold = 0 features_number = 0 while True: # main loop tracked_features = None while True: # detection loop, loop over the images unchangedPointsMap = dict() # load the image and resize it to (1) reduce detection time # and (2) improve detection accuracy (grabbed, current_frame) = camera.read() current_frame = imutils.resize(current_frame, width=300) current_frame_copy = current_frame.copy() current_frame = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY) # detect people in the image (rects, weights) = hog.detectMultiScale(current_frame, winStride=(4, 4), padding=(8, 8), scale=1.5) # draw the original bounding boxes for i in range(len(rects)): x, y, w, h = rects[i] rects[i][0] = x + 20 rects[i][1] = y + 10 rects[i][2] = w - 30 rects[i][3] = h - 10 for (x, y, w, h) in rects: cv2.rectangle(current_frame_copy, (x, y), (x + w, y + h), (0, 0, 255), 2) # apply non-maxima suppression to the bounding boxes using a # fairly large overlap threshold to try to maintain overlapping # boxes that are still people rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) # draw the final bounding boxes for (xA, yA, xB, yB) in pick: cv2.rectangle(current_frame, (xA, yA), (xB, yB), (0, 255, 0), 2) print("{} original boxes, {} after suppression".format( len(rects), len(pick))) if len(rects) > 0: features = find_features(current_frame, rects[0], 0) print("NUM" + str(features_number)) break # cv2.imshow("HOG", current_frame_copy) # key = cv2.waitKey(1) & 0xFF # if key == ord("w"): # break features_number = len(features) Threshold = features_number * threshold_percent while True: # Tracking loop #print ("Threshold" + str(Threshold)) if features_number < Threshold: print("Features less than threshold") break else: (grabbed, next_frame) = camera.read() next_frame = imutils.resize(next_frame, width=300) if not grabbed: print("Camera read failed") return current_frame_copy = next_frame.copy() next_frame = cv2.cvtColor(next_frame, cv2.COLOR_BGR2GRAY) #-----------Tracking using LK --------------------------- try: features = np.array(features) (tracked_features, status, feature_errors) = cv2.calcOpticalFlowPyrLK( current_frame, next_frame, features, None, **lk_params) arr_x = [] arr_y = [] for i in range(len(tracked_features)): f = tracked_features[i] x = f[0][0] y = f[0][1] arr_x.append(x) arr_y.append(y) arr_x = sorted(arr_x) arr_y = sorted(arr_y) mid = len(arr_x) / 2 X = arr_x[mid] mid = len(arr_y) / 2 Y = arr_y[mid] print("X Value " + str(X)) print("Y Value " + str(Y)) bus.write_byte(address, X) bus.write_byte(address, Y) new_feature_number = 0 temp_set_number = [] temp_distance = [] j = 0 for i in range(features_number): if status[i] == 1: new_feature_number += 1 j += 1 features_number = new_feature_number features = [] for i in range(features_number): features.append(tracked_features[i]) features = np.array(features) tracked_features = [] current_frame = next_frame.copy() except Exception, e: # bus.write_byte(address, 0) # bus.write_byte(address, 0) raise e #-------Showing Points ------------------------ for i in range(features_number): #print ("features " + str(features[i])) cv2.circle(current_frame_copy, tuple(features[i][0]), 3, 255, -1) cv2.circle(current_frame_copy, (X, Y), 5, (0, 0, 255), -1) # show the output images cv2.imshow("HOG", current_frame_copy) key = cv2.waitKey(1) & 0xFF if key == ord("w"): break
def main(): # uncomment only one # logging.basicConfig(format='%(message)s', level=logging.INFO) #logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) # load camera configuration try: settings = config.camera except AttributeError: settings = {} # send counters to Unix Domain Socket import uds # use Raspberry Pi Camera # camera = PiCamera() camera.resolution = (320, 240) rawCapture = PiRGBArray(camera, size=(320, 240)) time.sleep(0.1) camera.capture(rawCapture, format="bgr") image = rawCapture.array prev = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) np.zeros_like(prev) rawCapture.truncate(0) # initialise model for human faces # face_cascade = cv2.CascadeClassifier('models/haarcascade_frontalface_alt.xml') # initialise model for standing humans # hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # compute counters every 2 seconds # flagone = datetime.datetime.now() while True: flagtwo = datetime.datetime.now() elapsed = (flagtwo - flagone).total_seconds() if elapsed < 2: time.sleep(0.1) continue flagone = datetime.datetime.now() people_count = 0 people_moves = 0 people_faces = 0 # capture an image # mask = np.zeros(image.shape[:2], dtype='uint8') camera.capture(rawCapture, format='bgr') image = rawCapture.array gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) rawCapture.truncate(0) # detect human faces # faces = face_cascade.detectMultiScale(gray, 1.3, 5) people_faces = len(faces) # detect human beings # (rects, _) = hog.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.07) rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) people_count = len(pick) for(xA, yA, xB, yB) in pick: cv2.rectangle(mask, (xA, yA), (xB, yB), 255, -1) # count moving people # nextim = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) nextim = cv2.bitwise_and(nextim, nextim, mask=mask) flow = cv2.calcOpticalFlowFarneback(prev, nextim, None, 0.5, 1, 3, 15, 3, 5, 1) mag, _ = cv2.cartToPolar(flow[..., 0], flow[..., 1]) mag = mag * 0.8 prev = nextim for(xA, yA, xB, yB) in pick: try: m = np.median(mag[yA:yB, xA:xB]) # print m if m > 1.6: people_moves += 1 except RuntimeWarning: pass # push counters via Unix Domain Socket # message = '%s %d %d %d' % (settings.get('id', 'myCamera'), people_count, people_moves, people_faces) uds.push(message)
import cv2 import numpy as np if __name__ == '__main__': # 参数定义 PosNum = 820 NegNum = 1931 winSize = (64, 128) blockSize = (16, 16) # 105 bocks blockStride = (8, 8) # 4 cells cellSize = (8, 8) nBin = 9 # 1. hog创建 hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nBin) # 2. SVM创建 svm = cv2.ml.SVM_create() # 3. 计算hog featureNum = int( ((128 - 16) / 8 + 1) * ((64 - 16) / 8 + 1) * 4 * 9) # 3780 featureArray = np.zeros((PosNum + NegNum, featureNum), np.float32) labelArray = np.zeros((PosNum + NegNum, 1), np.int32) # 4. SVM 监督学习,样本, 标签, svm-> image hog for i in range(0, PosNum): fileName = 'pos/' + str(i + 1) + '.jpg' img = cv2.imread(fileName) hist = hog.compute(img, winStride=(8, 8))
def svmdetectperson(img): hog=cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) person,w= hog.detectMultiScale(img) return person
def run_kalman_filter(kf, imgs_dir, noise, sensor, save_frames={}, template_loc=None): imgs_list = [ f for f in os.listdir(imgs_dir) if f[0] != '.' and f.endswith('.jpg') ] imgs_list.sort() frame_num = 0 if sensor == "hog": hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) elif sensor == "matching": frame = cv2.imread(os.path.join(imgs_dir, imgs_list[0])) template = frame[template_loc['y']:template_loc['y'] + template_loc['h'], template_loc['x']:template_loc['x'] + template_loc['w']] else: raise ValueError("Unknown sensor name. Choose between 'hog' or " "'matching'") for img in imgs_list: frame = cv2.imread(os.path.join(imgs_dir, img)) # Sensor if sensor == "hog": (rects, weights) = hog.detectMultiScale(frame, winStride=(4, 4), padding=(8, 8), scale=1.05) if len(weights) > 0: max_w_id = np.argmax(weights) z_x, z_y, z_w, z_h = rects[max_w_id] z_x += z_w // 2 z_y += z_h // 2 z_x += np.random.normal(0, noise['x']) z_y += np.random.normal(0, noise['y']) elif sensor == "matching": corr_map = cv2.matchTemplate(frame, template, cv2.cv.CV_TM_SQDIFF) z_y, z_x = np.unravel_index(np.argmin(corr_map), corr_map.shape) z_w = template_loc['w'] z_h = template_loc['h'] z_x += z_w // 2 + np.random.normal(0, noise['x']) z_y += z_h // 2 + np.random.normal(0, noise['y']) x, y = kf.process(z_x, z_y) if False: # For debugging, it displays every frame out_frame = frame.copy() cv2.circle(out_frame, (int(z_x), int(z_y)), 20, (0, 0, 255), 2) cv2.circle(out_frame, (int(x), int(y)), 10, (255, 0, 0), 2) cv2.rectangle(out_frame, (int(z_x) - z_w // 2, int(z_y) - z_h // 2), (int(z_x) + z_w // 2, int(z_y) + z_h // 2), (0, 0, 255), 2) cv2.imshow('Tracking', out_frame) cv2.waitKey(1) # Render and save output, if indicated if frame_num in save_frames: frame_out = frame.copy() cv2.circle(frame_out, (int(x), int(y)), 10, (255, 0, 0), 2) cv2.imwrite(save_frames[frame_num], frame_out) # Update frame number frame_num += 1 if frame_num % 20 == 0: print 'Working on frame %d' % frame_num
def getROI_Array(img): hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) hogParams = {'winStride': (8, 8), 'padding': (16, 16), 'scale': 1.05} (rect, weights) = hog.detectMultiScale(img, **hogParams) return rect
import numpy as np import cv2 import time import cv2 import imutils import matplotlib.pyplot as plt #from laneDetection.py import canny_edge_detector car_cascade = 'cascades/haarcascade_car.xml' car_classifier = cv2.CascadeClassifier(car_cascade) pedestrain = cv2.HOGDescriptor() pedestrain.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) capture = cv2.VideoCapture('files/test3.mp4') while capture.isOpened(): response, frame = capture.read() if response: frame = imutils.resize(frame, width=min(700, frame.shape[1])) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) (regions, _) = pedestrain.detectMultiScale(frame, winStride=(4, 4), padding=(4, 4), scale=1.05) cars = car_classifier.detectMultiScale(gray, 1.2, 3) for (x, y, w, h) in cars: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 3) cv2.putText(frame, "Car Detected", (x, y - 5),
from outfitcmp.scripts.data_preprocessing_flow import read_splits, load_data_desc, SPLITS_NAMES WORKING_DIR = os.path.dirname(__file__) ROOT_DIR = os.path.join(WORKING_DIR, '..', '..', '..') DATA_DIR = os.path.join(ROOT_DIR, 'data') OUTPUT_DIR = os.path.join(ROOT_DIR, 'data', 'manual_features') SEGMENTATION_FILE = os.path.join(DATA_DIR, 'out_seg.h5') IMAGES_DIR = os.path.join(DATA_DIR, '144k', 'images') SEG_TYPES_COUNT = 25 SEG_IMG_SIZE = (400, 600) HOG_FEATURES_COUNT = 3780 HOG_IMG_SIZE = (64, 128) HOG_DESCRIPTOR = cv2.HOGDescriptor() def get_header(): """ Get header for for features """ header = ['label', 'image_name'] for t in range(SEG_TYPES_COUNT): header.append('seg_type_{}'.format(t)) for t in range(SEG_TYPES_COUNT): header.append('mean_B_{}'.format(t)) header.append('mean_G_{}'.format(t)) header.append('mean_R_{}'.format(t)) header.append('mean_L_{}'.format(t)) header.append('mean_A_{}'.format(t)) header.append('mean_B_{}'.format(t)) for t in range(HOG_FEATURES_COUNT):
# python 3.7 import cv2 from imutils.object_detection import non_max_suppression import numpy as np # Camera capture commands = 'raspistill -v -o ' + 'capture' + '.jpg' os.system(commands) img = cv2.imread("./capture.jpg") orig = img.copy() # Detect persons and resize their images defaultHog = cv2.HOGDescriptor() # Define HOG target defaultHog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector()) # Define SVN classifier (rects, weights) = defaultHog.detectMultiScale(img, winStride=(4, 4), padding=(8, 8), scale=1.05) rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) for (xA, yA, xB, yB) in pick: cv2.rectangle(img, (xA, yA), (xB, yB), (0, 255, 0), 2) cropImg = img[yA:yB, xA:xB] cv2.imwrite("./data/reid_robot/000.jpg", cropImg) print('Image Captured')
def __init__(self, preview): self.hog = cv2.HOGDescriptor() self.hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# *^_^* coding:utf-8 *^_^* __author__ = 'stone' __date__ = '15-11-11' import cv2 import numpy as np img = cv2.imread('../medias/smoke/positive/shot0052.png') winSize = (640, 480) padding = (8, 8) blockStride = (8, 8) descriptor = cv2.HOGDescriptor() hog = descriptor.compute(img, blockStride, padding) print type(hog) np.savetxt('hog.txt', hog)
# ------------------------ Define ------------------------# print("============================================== Start .") iFilePath = "./image_data/" path_list = [iFilePath + filename + '/' for filename in os.listdir(iFilePath)] hogParams = { '_winSize': (64, 64), '_blockSize': (16, 16), '_blockStride': (8, 8), '_cellSize': (8, 8), '_nbins': 9 } HoG = cv2.HOGDescriptor(**hogParams) print("=============================================== Model input . ") deep_model = {} # ------------------------ this path is your model path model_path = "./2021_08_10_01_32_54-useH-notUseR/" # ------------------------ model_name_list = os.listdir(model_path) model_name_list.sort(key=lambda m: len(m.split("-"))) for model_name in model_name_list: model_split = model_name[:-4].split("-")[1:] if len(model_split) == 0: deep_model = {"model": cv2.ml.SVM_load(model_path + model_name)}
def rearCarandPeopleDetector(gray, frame): #SETUP OBJECT DETECTORS: initialize detectors for pedestrians and cars #----------------------------------------------------------------------- database = 'C:\\Users\\estod_000\\Box\\EcoCAR 3\\Electrical\\ADAS\\Current Projects\\Object Detection\\Object Detectors' pedDet = cv2.HOGDescriptor() pedDet.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) rearDet = cv2.CascadeClassifier('rearVehicleDetectorHaar_700sample.xml') carDet = cv2.CascadeClassifier('cars.xml') #VARIABLES AND OBJECTS #----------------------- i = 1 validCarsFound = 0 #TODO limit Cars found to just 5? or allow as many as possible AreafilteredCars = [] #1. REGION OF INTEREST(ROI): create ROI for middle row of screen #---------------------------------------------------------------- height, width = gray.shape ROI2end = (int)(5 * height / 6) ROIstart = (0.5225 * ROI2end) ROI1end = (int)(0.7 * ROI2end) gray2 = gray[(int)(ROIstart):(int)(ROI2end), 0:(int)(width)] gray1 = gray[(int)(ROIstart):(int)(ROI1end), 0:(int)(width)] cv2.rectangle( frame, (0, (int)(ROIstart)), ((int)(width), (int)(ROI2end)), (255, 255, 0), 2) # can return "frame" depending on version. Used to view ROI cv2.rectangle( frame, (0, (int)(ROIstart)), ((int)(width), (int)(ROI1end)), (200, 200, 0), 2) # can return "frame" depending on version. Used to view ROI #2. CAR OBJECT DETECTION: detect cars #----------------------------------- rearOnline = carDet.detectMultiScale(gray1, 1.1, 2) rearFound = rearDet.detectMultiScale(gray2, scaleFactor=1.05, minNeighbors=8, minSize=(1, 1), flags=cv2.CASCADE_SCALE_IMAGE) #3. PEDESTRIAN DETECTION: detect pedestrians #----------------------------------------111 pedFound, pedW = pedDet.detectMultiScale(gray, winStride=(8, 8), padding=(32, 32), scale=1.05) for (pedX, pedY, pedW, pedH) in pedFound: pedY += (int)(ROIstart) #adjust ROI offset for displaying on "frame" if (pedY <= 266): cv2.rectangle(frame, (pedX, pedY), (pedX + pedW, pedY + pedH), (255, 255, 255), 2) #4. AREA FILTER: filter cars based on the size of the object and its location on the screen #------------------------------------------------------------------------------------------ for (rx, ry, rw, rh) in rearOnline: ry += (int)((ROIstart) + 1 / 3.0 * rh) rx += (int)(1 / 4.0 * rw) rw = (int)(rw / 2.0) rh = (int)(rh / 3.0) area = rw * rh #print "area: " +str(area) + " with y: " + str(ry) #TODO far = (ry <= 240) and area <= 31000 farther = (ry >= 241 and ry <= 258) close = (ry >= 259 and ry <= 266) and area > 2800 and area <= 1000 if not (farther or far or close): # AreafilteredCars.append([(int)(rx), (int)(ry), (int)(rw), (int)(rh)]) fileToSave = frame[(int)(ry):(int)(ry + rh), (int)(rx):(int)(rx + rw)] # name = "images/isCar/image_" + (str)(time.time()) + ".jpeg" # cv2.imwrite(name, fileToSave) #cv2.rectangle(frame, (rx, ry), ((rx+rw), (ry+rh)), (0, 255, 255), 2) # can return "frame" depending on version fileToSave = frame[(int)(ry):(int)(ry + rh), (int)(rx):(int)(rx + rw)] name = "images/trash-isNotCar/image_" + (str)( time.time()) + ".jpeg" cv2.imwrite(name, fileToSave) for (rearX, rearY, rearW, rearH) in rearFound: rearY += (int)(ROIstart) #adjust ROI offset for displaying on "frame" area = rearW * rearH close = (rearY >= 210 and rearY <= 226) and area > 2000 and area <= 12000 far = (rearY >= 227 and rearY <= 260) and area > 2000 and area <= 31000 farther = (rearY >= 261 and rearY <= 266) and area > 2800 and area <= 10000 if not (farther or far or close): #AreafilteredCars.append([rearX,rearY,rearW,rearH]) #fileToSave = frame[(int)(rearY):(int)(rearY+rearH), (int)(rearX):(int)(rearX+rearW)] #name = "images/isCar/image_" + (str)(time.time()) + ".jpeg" #cv2.imwrite(name, fileToSave) fileToSave = frame[(int)(rearY):(int)(rearY + rearH), (int)(rearX):(int)(rearX + rearW)] name = "images/trash-isNotCar/image_" + (str)( time.time()) + ".jpeg" cv2.imwrite(name, fileToSave) #5. OVERLAP FILTER: filter objects that overlap. only display the first object of overlapping objects. #-------------------------------------------------------------------------------------------------- #frame = overlapFilter(AreafilteredCars, frame, 0, 255, 255) #6: RETURN: return frame with bounding boxes #---------------------------------------- return frame
__author__ = 'Shuran' import cv2 import time import help from imutils import paths import numpy as np from sklearn import svm from sklearn.externals import joblib svm_params = dict(kernal_type=cv2.SVM_LINEAR, svm_type=cv2.SVM_C_SVC, C=1, gamma=0.5) classifier = svm.SVC(kernel='linear', probability=True) (winW, winH) = (100, 250) hog = cv2.HOGDescriptor((64, 64), (16, 16), (8, 8), (8, 8), 9) trainData = [] label = []
def get_frame(self, countif): global previous_frame global min_area global center global count global boxcolor global hog global frame_processed global timeout global font global center_fix global flag_track2 global prev_x_pixel global prev_y_pixel global counttrack2 global tetaperpixel global nucleo global distance global distance2 global teta global i global b global flag global pick2 global tracker global prev_distance2 def detect_people(frame, center, frame_out, bboxcolor=(0, 255, 0)): """ detect humans using HOG descriptor Args: frame: Returns: processed frame, center of every bb box """ centerxd = [] (rects, weights) = hog.detectMultiScale(frame, winStride=(8, 8), padding=(16, 16), scale=1.06) rects = non_max_suppression(rects, probs=None, overlapThresh=0.65) for (x, y, w, h) in rects: cv2.rectangle(frame_out, (x, y), (x + w, y + h), (0, 0, 255), 2) # apply non-maxima suppression to the bounding boxes using a # fairly large overlap threshold to try to maintain overlapping # boxes that are still people rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) idx = 0 # draw the final bounding boxes for (xA, yA, xB, yB) in pick: cv2.rectangle(frame_out, (xA, yA), (xB, yB), bboxcolor, 2) cv2.putText(frame_out, 'Person ' + str(idx), (xA, yA - 10), 0, 0.3, bboxcolor) idx = idx + 1 # calculate the center of the object centerxd.append([(xA + xB) / 2, (yA + yB) / 2]) return (frame, centerxd, pick) def background_subtraction(previous_frame, frame_resized_grayscale, min_area): """ This function returns 1 for the frames in which the area after subtraction with previous frame is greater than minimum area defined. Thus expensive computation of human detection face detection and face recognition is not done on all the frames. Only the frames undergoing significant amount of change (which is controlled min_area) are processed for detection and recognition. """ frameDelta = cv2.absdiff(previous_frame, frame_resized_grayscale) thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1] thresh = cv2.dilate(thresh, None, iterations=2) im2, cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) temp = 0 for c in cnts: # if the contour is too small, ignore it if cv2.contourArea(c) > min_area: temp = 1 return temp def encodex(x_pixel): if (x_pixel < 130): x_norm = 1 elif (x_pixel > 230): x_norm = 2 else: x_norm = 0 return x_norm if (countif < 1): # setup the serial port nucleo = serial.Serial() nucleo.port = '/dev/ttyACM0' nucleo.baud = 115200 nucleo.close() nucleo.open() nucleo.flush() time.sleep(2) print("connected to: " + nucleo.portstr) print("Running...") subject_label = 1 font = cv2.FONT_HERSHEY_SIMPLEX tracker = KCF.kcftracker( True, False, True, False) # hog, fixed_window, multiscale, lab # initialize the HOG descriptor/person detector hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) time.sleep(1) frame = np.zeros((480, 640, 3), np.uint8) flag_track2 = 0 count = 0 counttrack2 = 0 prev_y_pixel = 0 prev_x_pixel = 0 tetaperpixel = 0.994837 / 400.0 prev_distance2 = 0 # grab one frame at first to compare for background substraction frame, timestamp = freenect.sync_get_video() #time.sleep(5) frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) frame_resized = imutils.resize(frame, width=min(400, frame.shape[1])) frame_resized_grayscale = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2GRAY) # initialize centroid center = [[frame_resized.shape[1] / 2, frame_resized.shape[0] / 2]] center_fix = [] # defining min cuoff area #min_area=(480/400)*frame_resized.shape[1] min_area = (0.01) * frame_resized.shape[1] boxcolor = (0, 255, 0) timeout = 0 #variable for counting time elapsed temp = 1 previous_frame = frame_resized_grayscale # retrieve new RGB frame image # Frame generation for Browser streaming with Flask... self.outframe = open("stream.jpg", 'wb+') cv2.imwrite("stream.jpg", frame) # Save image... return self.outframe.read() else: # start timer timer = cv2.getTickCount() starttime = time.time() frame, timestamp = freenect.sync_get_video() frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) frame_resized = imutils.resize(frame, width=min(400, frame.shape[1])) frame_resized_grayscale = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2GRAY) temp = background_subtraction(previous_frame, frame_resized_grayscale, min_area) # retrieve depth map depth, timestamp = freenect.sync_get_depth() depth = imutils.resize(depth, width=min(400, depth.shape[1])) depth2 = np.copy(depth) # orig = image.copy() if temp == 1: if (flag_track2 == 0): frame_processed, center_fix, pick2 = detect_people( frame_resized_grayscale, center, frame_resized, boxcolor) if (len(center_fix) > 0): i = 0 for b in center_fix: #print(b) #print("Point "+str(i)+": "+str(b[0])+" "+str(b[1])) x_pixel = b[1] y_pixel = b[0] rawDisparity = depth[(int)(x_pixel), (int)(y_pixel)] print("raw:" + str(rawDisparity)) distance = 1 / (-0.00307 * rawDisparity + 3.33) if (distance < 0): distance = 0.5 print("Distance : " + str(distance)) cv2.putText(frame_resized, "distance: {:.2f}".format(distance), (10, (frame_resized.shape[0] - (i + 1) * 25) - 50), font, 0.65, (0, 0, 255), 3) cv2.putText( frame_resized, "Point " + str(i) + ": " + str(b[0]) + " " + str(b[1]), (10, frame_resized.shape[0] - (i + 1) * 25), font, 0.65, (0, 0, 255), 3) i = i + 1 y_pix, x_pix = center_fix[0] endtime = time.time() #nucleo.write(("8,"+str(x_person)+","+str(y_person)).encode()) # send x_person and y_person if ((abs(prev_x_pixel - x_pix)) < 50 and (abs(prev_y_pixel - y_pix)) < 50): timeout = timeout + (endtime - starttime) if (timeout > 5): flag_track2 = 1 boxcolor = (255, 0, 0) else: nucleo.flush() nucleo.write("8,,,,,,,,,,,,".encode()) timeout = 0 boxcolor = (0, 255, 0) prev_y_pixel, prev_x_pixel = y_pix, x_pix # DEBUGGING # #print("Teta: " + str(teta) + "Distance: " + str(distance)) print("Timeout: " + str(timeout)) #print ("Distance : " + str(distance)) elif (len(center_fix) <= 0): timeout = 0 boxcolor = (0, 255, 0) nucleo.flush() nucleo.write("8,,,,,,,,,,,,".encode()) elif (flag_track2 == 1): if (counttrack2 == 0): iA, iB, iC, iD = pick2[0] tracker.init([iA, iB, iC - iA, iD - iB], frame_resized) counttrack2 = counttrack2 + 1 elif (counttrack2 == 1): print(pick2[0]) #print("iA:"+str(iA)+"iB:"+str(iB)+"iC:"+str(iC)+"iD:"+str(iD)) boundingbox = tracker.update( frame_resized) #frame had better be contiguous boundingbox = list(map(int, boundingbox)) cv2.rectangle(frame_resized, (boundingbox[0], boundingbox[1]), (boundingbox[0] + boundingbox[2], boundingbox[1] + boundingbox[3]), (255, 0, 0), 3) #GENERAL ASSUMPTION SINGLE PERSON TRACKING # start tracking... x_track = ((boundingbox[2]) / 2.0) + boundingbox[0] y_track = ((boundingbox[3]) / 2.0) + boundingbox[1] print("x:" + str(x_track) + "y:" + str(y_track)) x_center = (frame_resized.shape[1] + 1) / 2 y_center = (frame_resized.shape[0] + 1) / 2 print(x_center, y_center) # compute teta asumsi distance lurus rawDisparity2 = depth2[(int)(y_track), (int)(x_track)] print("raw2:" + str(rawDisparity2)) distance2 = 1 / (-0.00307 * rawDisparity2 + 3.33) if (distance2 < 0): distance2 = prev_distance2 prev_distance2 = distance2 #realx = (x_track-x_center)+(distance/30.0) #teta = math.atan(realx/distance) # if distance is tangensial #teta = math.asin((0.026458333*(x_track-x_center)/distance)) # if distance is euclidean teta = (x_track - x_center) * tetaperpixel print("teta2: " + str(teta)) print("Distance2 : " + str(distance2)) cv2.putText(frame_resized, "distance: {:.2f}".format(distance2), (10, (frame_resized.shape[0] - (i + 1) * 25) - 50), font, 0.65, (0, 0, 255), 3) cv2.putText( frame_resized, "Point " + str(0) + ": " + str(x_track) + " " + str(y_track), (10, frame_resized.shape[0] - (i + 1) * 25), font, 0.65, (0, 0, 255), 3) # send the teta and distance nucleo.flush() if (teta < 0.0): flag = nucleo.write( ("7," + format(teta, '1.2f') + "," + format(distance2, '1.3f')).encode()) elif (teta > 0.0): flag = nucleo.write( ("7," + format(teta, '1.3f') + "," + format(distance2, '1.3f')).encode()) print("WRITEIN1" + str(flag)) if (tracker.getpeakvalue() < 0.4): counttrack2 = 0 flag_track2 = 0 nucleo.flush() nucleo.write("8,,,,,,,,,,,,".encode()) print("WRITEOUT") #frame_resized = cv2.flip(frame_resized, 0) #cv2.imshow("Detected Human", frame_resized) #cv2.imshow("Depth", depth) # cv2.imshow("Original", frame) else: count = count + 1 #print("Number of frame skipped in the video= " + str(count)) # compute the fps fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) #print("FPS: " + str(fps)) # Frame generation for Browser streaming with Flask... self.outframe = open("stream.jpg", 'wb+') cv2.imwrite("stream.jpg", frame_resized) # Save image... return self.outframe.read()
def get_hog(): hog = cv2.HOGDescriptor() hog.load('hog_descriptor.xml') return hog
def computeHOG(image): rows, cols = image.shape WINDOW_SIZE = (cols, rows) hog = cv.HOGDescriptor(WINDOW_SIZE,BLOCK_SIZE,BLOCK_STRIDE,CELL_SIZE,NBINS) output = hog.compute(image) return output.flatten()
def calcDwellTime(self, parameters): # segregate different parameters from list #print("pppppppp ",len(parameters)) videostream = parameters[0] config = parameters[1] if not parameters[2]: livestream = False else: livestream = parameters[2] if not parameters[3]: isnovideo = False else: isnovideo = parameters[3] if not parameters[4]: starttime = None else: starttime = parameters[4] if not parameters[5]: frameskip = 0 else: frameskip = parameters[5] if not parameters[6]: flushtime = 1000 else: flushtime = parameters[6] if not parameters[7]: output = None else: output = parameters[7] if not parameters[8]: debug = False else: debug = parameters[8] video = videostream cleartime = 100 updatetime = 1000 if cleartime <= 0: cleartime = 1000 verbose = debug novideo = isnovideo totalframes = 0 fps = 0 videolength = 0 currentframetime = 0 remainingtime = 0 prevcentroids = [] centroids = [] currentflow = [] lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) database = [] first = True cap = None clearthreadtimer = 0 #use this variable as a timer to start the thread for clearing the in database updatethreadtimer = 0 incount = 0 font = cv2.FONT_HERSHEY_SIMPLEX framenumber = 0 names = [] boxes = [] result = [None] dwelldatabase = {} clearthread = Thread(target=self.garbageClear, args=(database, 30, dwelldatabase, starttime), name="clear_thread") # in database clear thread #updatethread=Thread(target=self.updateInMDB,args=(self.db,"dwell",dwelldatabase,result),name="update_thread") try: conf = [] conf.append( config ) # bind the configuration dict obtained from function parameter into a List width = 250 if len(conf) > 0: for seg in conf: names.append(seg['name']) # segment name boxes.append( seg['coordinates'] ) # segment box coordinates (diagonally opposite vertices) else: print("No segment defined in the configuration file") return -1 for n in names: dwelldatabase[n] = [] # initialise HOG descriptor for people detecting hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) if not livestream: cap = cv2.VideoCapture(video) fps = cap.get(cv2.CAP_PROP_FPS) if first == True: r, prevframe = cap.read() if not r: print("video finished ") raise (Exception("Video Finished")) first = False currentframetime = cap.get(cv2.CAP_PROP_POS_MSEC) remainingtime = videolength - currentframetime framenumber = cap.get(cv2.CAP_PROP_POS_FRAMES) totalframes = cap.get(cv2.CAP_PROP_FRAME_COUNT) print(totalframes) videolength = (totalframes / fps) * 1000 # in seconds elif livestream: # read the stream from IP camera import urllib.request ip_stream = urllib.request.urlopen( videostream) # videostream corresponds to http URI print(ip_stream.headers['Content-Length']) if ip_stream.status != 200: raise (Exception("Unable to fetch video from the IP")) totalframes = 0 videolength = 0 global bytes bytes = bytes() #print(ip_stream.read()) # ======================================================================== while True: ret = True if livestream: # read individual frame from ip_stream bytes += ip_stream.read(65536) a = bytes.find(b'\xff\xd8') b = bytes.find(b'\xff\xd9') print(b) if a != -1 and b != -1: jpg = bytes[a:b + 2] bytes = bytes[b + 2:] frame = cv2.imdecode( np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR) if first: prevframe = frame.copy() first = False else: raise (Exception("Cannot load stream from IP")) else: (ret, frame) = cap.read() framenumber = cap.get(cv2.CAP_PROP_POS_FRAMES) currentframetime = cap.get(cv2.CAP_PROP_POS_MSEC) if not ret: framenumber = cap.get(cv2.CAP_PROP_POS_FRAMES) print("video finished") cv2.destroyAllWindows() cap.release() raise (Exception("Video Finished")) orig = frame.copy() # make a copy of the current frame frame = imutils.resize(frame, width=min( width, frame.shape[1])) #resize the frame prevframe = imutils.resize( prevframe, width=min(width, prevframe.shape[1])) #resize previous frame (prevrects, prevweights) = hog.detectMultiScale( prevframe, winStride=(4, 4), padding=(4, 4), scale=1.05) #detect human in previous frame print(prevrects) print(prevweights) i = 0 if len(prevrects) > 0 and len(prevweights) > 0: prevweights = prevweights.tolist() prevrects = prevrects.tolist() while (i < len(prevweights)): if prevweights[i][0] < float(1): prevweights.pop(i) prevrects.pop(i) i -= 1 i += 1 prevrects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in prevrects]) prevweights = np.array(prevweights) prevpick = non_max_suppression(prevrects, probs=None, overlapThresh=0.65) if len(prevpick) > 0: #and len(pick) > 0: if verbose: print("seg 1") for (xa, ya, xb, yb) in prevpick: cen = cu.getCentroid([(xa, ya), (xb, yb)]) centroids.append(cen) if verbose: print("seg 2") i = -1 for c in centroids: i += 1 cx = int(c[0]) cy = int(c[1]) p0 = np.array([[cx, cy]], np.float32) p0x = int(p0[0][0]) p0y = int(p0[0][1]) p0 = (p0x, p0y) prevbox = [[prevpick[i][0], prevpick[i][1]], [prevpick[i][2], prevpick[i][3]]] index = -1 index = cu.pointInWhichBox(p0, boxes) if index != -1: #in the person is in any of the segments name = names[index] print(name) print(dwelldatabase) print( self.insertPerson( prevbox, cen, database, name, cap.get(cv2.CAP_PROP_POS_MSEC) / 1000, len(dwelldatabase[name]))) # insert person elif index == -1: status, data = self.removePerson( prevbox, cen, database) #remove person if status: print(data) segname = data[3][0] if starttime != None and starttime != 0: print(starttime) personin = starttime + datetime.timedelta( seconds=data[4][0]) personin = personin.__str__() personout = starttime + datetime.timedelta( seconds=data[5][0]) personout = personout.__str__() else: personin = str(data[4][0]) personout = str(data[5][0]) personincount = data[7][0] personoutcount = data[8][0] partialdata = data[9][0] totalsec = data[6][0] #d=[segname,personin,personout,totalsec,personincount,personoutcount,partialdata] d = { "segname": segname, "personin": personin, "personout": personout, "totalsec": totalsec, "personincount": personincount, "personoutcount": personoutcount, "partialdata": partialdata } dwelldatabase[segname].append(d) cv2.circle(frame, p0, 5, (0, 0, 255), -1) for (xa, ya, xb, yb) in prevpick: cv2.rectangle(frame, (xa, ya), (xb, yb), (0, 255, 0), 2) cen = cu.getCentroid([(xa, ya), (xb, yb)]) cv2.circle(frame, cen, 5, (0, 255, 0), -1) for b in boxes: cv2.rectangle(frame, (b[0][0], b[0][1]), (b[1][0], b[1][1]), (0, 255, 0), 2) prevframe = orig.copy() prevcentroids = [] centroids = [] #print("dwelldatabase 1 : ",dwelldatabase) #input() print(clearthreadtimer, " / ", cleartime) #print("database : ",dwelldatabase) #input() if (clearthreadtimer >= cleartime): if (len(database) > 0): print(database) #input() clearthread = Thread( target=self.garbageClear, args=(database, 30, dwelldatabase, starttime), name="clear_thread") # database clear thread if verbose: print("clear thread starting ") clearthread.start() clearthread.join() if verbose: print("clear thread finished") del (clearthread) clearthread = None clearthreadtimer = 0 #print("dwelldatabase 2 : ",dwelldatabase) #print(type(dwelldatabase)) #input() print(updatethreadtimer, " / ", updatetime) if (updatethreadtimer >= updatetime): if len(dwelldatabase) > 0: #updatethread=Thread(target=self.updateInMDB,args=(self.db,"dwell",dwelldatabase,result),name="update_thread") if verbose: print("update thread starting ") res = self.updateInMDB(self.db, "dwell", dwelldatabase) #dwelldatabase.clear() for k in dwelldatabase: dwelldatabase[k] = [] #dwelldatabase.pop('_id') #print (res) #dwelldatabase.clear() if verbose: print("update thread finished") updatethreadtimer = 0 print("dwelldatabase 3 : ", dwelldatabase) #input() #print(type(dwelldatabase)) clearthreadtimer += 1 #use this variable as a timer to start the thread for clearing the database updatethreadtimer += 1 if not novideo: cv2.imshow(str(video), frame) k = cv2.waitKey(1) & 0xFF if k == ord("q"): framenumber = cap.get(cv2.CAP_PROP_POS_FRAMES) cv2.destroyAllWindows() cap.release() raise (Exception("Video Finished")) except Exception as e: print("Fatal Error in dwellTime()") print("Error Name : ", e) print("Error in Details :") if verbose: err = sys.exc_info() print("Error Type : ", err[0]) print("file name : ", err[-1].tb_frame.f_code.co_filename) print("Line Number : ", err[-1].tb_lineno) finally: cv2.destroyAllWindows() if cap: if framenumber <= 0: framenumber = cap.get(cv2.CAP_PROP_POS_FRAMES) print("exiting ", framenumber) cap.release() return dwelldatabase, framenumber, fps
def hog_features(image): hog_computer = cv2.HOGDescriptor() return hog_computer.compute(image)
action="store", help="directorio de archivo a procesar") p.add_argument("-m", "--model", default="model.svm", action="store", help="Modelo de SVM") opts = p.parse_args() listing = os.listdir(opts.positivos) listing = [ "{0}/{1}".format(opts.positivos, namefile) for namefile in listing if namefile.endswith('jpg') or namefile.endswith('png') ] hog = cv2.HOGDescriptor((48, 48), (16, 16), (8, 8), (8, 8), 9) trainData = [] print "Generating samples" for filename in listing: # Se abre la imagen img = cv2.imread(filename) #gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) des = hog.compute(img) trainData.append((des, 1)) listing = os.listdir(opts.negativos) listing = [ "{0}/{1}".format(opts.negativos, namefile) for namefile in listing if namefile.endswith('jpg') or namefile.endswith('png')