def detect_image(integer,lokl,eventol,evento2l,the_queuel,the_final_queuel,threshl,hier_threshl,darknet_pathl, data_filel, cfg_filel, weight_filel): # Inputs: # ======= # the_queuel(multiprocessing.Queue) the queue where the images waits to be processed. # hier_thresl (float) # thresl (float) this parameters define the minimun probability that will be accepted as a detection. # darknet_pathl (str) path to darknet folder. # data_filel (str) path to data file. # cfg_filel (str) path to condiguration file. # weight_filel (str) path to weight file. # Returns: # ======== # outputs (list) every entry list is a dictionary with keys=['right','left','top','bottom','class','prob'] added to a queue # Load YOLO weight pyyolo.init(darknet_pathl, data_filel, cfg_filel, weight_filel)#loading darknet in the memory #while True: while the_queuel.qsize()>0 or eventol.is_set()==False: from_queue = the_queuel.get() #note that every item in queuel is a list with the following form [w, h, c, data,frame_id,img_rgb] outpyyolo=pyyolo.detect(from_queue[0], from_queue[1], from_queue[2], from_queue[3], threshl, hier_threshl) the_queuel.task_done() the_final_queuel.put([outpyyolo,from_queue[4],from_queue[5]]) pyyolo.cleanup() print("tamagno de la cola cuando termino el proceso detector ",integer ," es ", the_queuel.qsize()) print("El proceso detector",integer,"ve que evento1 termino? ",eventol.is_set()) if evento2l.is_set()==False: evento2l.set()
def image_cb(self, image_msg, depth_msg): rospy.loginfo('Received images') det_conns = self.det_pub.get_num_connections() pred_conns = self.pred_pub.get_num_connections() # Early return if no one is listening if not det_conns and not pred_conns: return # The retrieved image in ros_data is reshaped from a flat structure and normalized data = self.__prepare_image(image_msg) output = pyyolo.detect(self.frame_width, self.frame_height, 3, data, self.conf_thresh, self.hier_thresh) frame = self.bridge.imgmsg_to_cv2(image_msg, desired_encoding="passthrough") depth = self.bridge.imgmsg_to_cv2(depth_msg, desired_encoding="passthrough") predictions = PredictionContainer(output, image_msg.header, depth) if det_conns: predictions.draw(frame) image_msg = self.bridge.cv2_to_imgmsg(frame, encoding="passthrough") self.det_pub.publish(image_msg) if pred_conns: pred_msg = predictions.to_msg() self.pred_pub.publish(pred_msg)
def predict(img): img = img.transpose(2, 0, 1) c, h, w = img.shape[0], img.shape[1], img.shape[2] # print w, h, c data = img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) outputs = pyyolo.detect(w, h, c, data, thresh, hier_thresh) return outputs
def detect_image(filepath): img = cv2.imread(filepath) img = img.transpose(2, 0, 1) c, h, w = img.shape[0], img.shape[1], img.shape[2] data = img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) return pyyolo.detect(w, h, c, data, thresh, hier_thresh)
def _inference_image(self, image): frame = image.transpose(2, 0, 1) c, h, w = frame.shape data = frame.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) detects = pyyolo.detect(w, h, c, data, self.params['thresh'], self.params['hier_thresh']) return detects
def get_objs(self, image, thresh): if pyyolo is None: return None else: img = image.transpose(2, 0, 1) c, h, w = img.shape[0], img.shape[1], img.shape[2] data = img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) objs = pyyolo.detect(w, h, c, data, thresh, self.hier_thresh) return objs
def detect(): global image img = image.transpose(2, 0, 1) c, h, w = img.shape[0], img.shape[1], img.shape[2] data = img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) outputs = pyyolo.detect(w, h, c, data, 0.24, 0.5) #thresh, heir_thresh return outputs
def overlay(img): proc_img = img.transpose(2, 0, 1) c, h, w = proc_img.shape data = proc_img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) outputs = pyyolo.detect(w, h, c, data, THRESH, HIER_THRESH) for output in outputs: topleft = (output['left'], output['top']) bottomright = (output['right'], output['bottom']) color = (int(255 * (1 - output['prob'])), 0, int(255 * output['prob'])) img = cv2.rectangle(img, topleft, bottomright, color, 10) img = cv2.putText(img, output['class'], (output['left'] + 10, output['bottom'] - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, color, 2, cv2.LINE_AA) print(output) return img
def detection_yolo(datacfg, cfgfile, weightfile, imgfile): pyyolo.init(datacfg, cfgfile, weightfile) # image catograys = {} img = cv2.imread(imgfile) img = img.transpose(2, 0, 1) c, h, w = img.shape[0], img.shape[1], img.shape[2] #print c, h, w data = img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) outputs = pyyolo.detect(w, h, c, data, thresh, hier_thresh) #print outputs res = convertoutputyolo(outputs, w, h) pyyolo.cleanup() return res
def predict(self, image_base64, out_dir): """Public function for performing template detection on input base64 image, This function uses pyyolo api for calling darknet object detector and returns detected templates in python dictionary :param object: base class inheritance :type object: class:`Object` :param image_base64: input image file :type image_base64: base64 string format :param out_dir: directory where image will be saved for debug :type out_dir: string :return: outputs , w , h (dictionary containing bounding box of detected templates, input image width input image height) :rtype: dictionary, int , int """ # Save base64 string as image image = utils.base64_to_image(image_base64) input_image_name = self._save_file_on_disk(image, out_dir) # Load image input_image = cv2.imread(input_image_name) # Preprocess image img = input_image.transpose(2, 0, 1) c, h, w = img.shape[0], img.shape[1], img.shape[2] data = img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) # Detect elements components_detection_list = pyyolo.detect(w, h, c, data, self.thresh, self.hier_thresh) # Draw bboxes output_image = utils.draw_bboxes(input_image, components_detection_list) # Save output image output_image_name = ( os.path.splitext(os.path.basename(input_image_name))[0] + "_out.png") output_image_name = os.path.join(out_dir, output_image_name) cv2.imwrite(output_image_name, output_image) print("Results saved as", output_image_name) return components_detection_list, w, h
def predict_video(filename): cap = cv2.VideoCapture(filename) # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'MP4V') out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (1920, 1080)) while cap.isOpened(): ret, frame = cap.read() if not ret: # out of frames break c, h, w, data = prepare_img(frame) outputs = pyyolo.detect(w, h, c, data, thresh, hier_thresh) draw_bounding_boxes(frame, outputs) out.write(frame) cap.release() out.release()
def predict_sample_image(): # From file print('----- test original C using a file') outputs = pyyolo.test(filename, thresh, hier_thresh, 0) for output in outputs: print(output) # Camera print('----- test python API using a file') i = 1 while i < 2: # ret_val, img = cam.read() orig_img = cv2.imread(filename) c, h, w, data = prepare_img(orig_img) outputs = pyyolo.detect(w, h, c, data, thresh, hier_thresh) draw_bounding_boxes(orig_img, outputs) cv2.imwrite("predicted.jpg", orig_img) i = i + 1
def callback(self, ros_data): start = time.time() # The retrieved image in ros_data is reshaped from a flat structure and normalized self.image = np.fromstring(ros_data.data, np.uint8) image = self.image image = image.reshape((self.h, self.w, 3)) image = image.transpose(2, 0, 1) data = image.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) # The image, width, height, channels and thresholds are passed into the object detection network outputs = pyyolo.detect(self.w, self.h, self.c, data, self.thresh, self.hier_thresh) preds = Predictions() # The edges of the bounding boxes of each object is read and depth values calculated, the median of the depth values inside the box is determined as the objects distance for output in outputs: self.calculate_depth(output) self.calculate_coordinates(output) #print output pred = Prediction() pred.probabilities.append(output['prob']) pred.classes.append(output['class']) pred.xmin = output['left'] pred.ymin = output['top'] pred.xmax = output['right'] pred.ymax = output['bottom'] pred.distance = output['distance'] pred.angle = output['angle'] pred.xcoord = output['x'] pred.ycoord = output['y'] # self.pred_topic.publish(pred) preds.predictions.append(pred) self.pred_topic.publish(preds) end = time.time() print("Total cycle time: ", end - start)
def interaction_detector(rgb, depth, ir): persons = [] objects = [] homog = get_pos() depthm = np.mean(depth[70:170, 110:210]) rgbhomog = homog.rgb_conv(rgb, depthm) # convert rgb with homography rgbnew = rgb.transpose(2, 0, 1) c, h, w = rgbnew.shape[0], rgbnew.shape[1], rgbnew.shape[2] data = rgbnew.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) outputs = pyyolo.detect( w, h, c, data, thresh, hier_thresh) # output dictionary of objects classified for output in outputs: valid, out_homog = correct_bounds( output, depthm) # check to see if object is in homography view if valid: cv2.rectangle(rgbhomog, (out_homog['left_h'], out_homog['top_h']), (out_homog['right_h'], out_homog['bottom_h']), (255, 0, 0), 1) if output['class'] == 'person': persons.append(out_homog) #create set of humans else: objects.append(out_homog) #create set of objects if (len(persons) > 0 and len(objects) > 0): interacting( persons, objects, ir, depth ) #if there is atleast 1 object and 1 person see if they are interacting if (len(persons) == 0): print("No person detected") if (len(objects) == 0): print("No object detected") # cv2.imshow("boxeshomog", rgbhomog) # cv2.waitKey(0) return rgbhomog
def GigeStreamer(cam_id): camera_id = cam_id pyyolo.init(darknet_path, datacfg, cfgfile, weightfile) Aravis.enable_interface(camera_id) # using arv-fake-gv-camera-0.6 camera = Aravis.Camera.new(None) stream = camera.create_stream(None, None) payload = camera.get_payload() for i in range(0, 50): stream.push_buffer(Aravis.Buffer.new_allocate(payload)) print("Starting acquisition") camera.start_acquisition() while True: buffer = stream.try_pop_buffer() print(buffer) if buffer: frame = convert(buffer) stream.push_buffer(buffer) #push buffer back into stream cv2.imshow("frame", frame) # img = cv2.imread(filename) img = frame.transpose(2, 0, 1) # img = img.transpose(2,0,1) c, h, w = img.shape[0], img.shape[1], img.shape[2] data = img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) # perform_recognition() outputs = pyyolo.detect(w, h, c, data, thresh, hier_thresh) for output in outputs: print(output) ch = cv2.waitKey(1) & 0xFF if ch == 27 or ch == ord('q'): break elif ch == ord('s'): cv2.imwrite("imagename.png", frame) camera.stop_acquisition() pyyolo.cleanup()
cam = cv2.VideoCapture(filename) ret_val, img = cam.read() print(ret_val) if not ret_val: sys.exit() pyyolo.init(darknet_path, datacfg, cfgfile, weightfile) fourcc = cv2.VideoWriter_fourcc(*'XVID') outVideo = cv2.VideoWriter('outputRoboySkyfall.mp4',fourcc, 20.0, (800,533)) print('----- test python API using a file') while True: ok,img = cam.read() frame = imutils.resize(img, width=800) if not img.any(): sys.exit() img = img.transpose(2,0,1) c, h, w = img.shape[0], img.shape[1], img.shape[2] print w, h, c data = img.ravel()/255.0 data = np.ascontiguousarray(data, dtype=np.float32) outputs = pyyolo.detect(w, h, c, data, thresh, hier_thresh) for output in outputs: print(output) p1 = (output['left'],output['top']) p2 = (output['right'],output['bottom']) print(p1,p2) cv2.rectangle(frame,p1,p2,(0,0,255,10)) outVideo.write(frame) # free model pyyolo.cleanup()
File name: example.py Author: rameshpr Date: 10/29/18 """ import cv2 import numpy as np import pyyolo names_filepath = "./coco.names" cfg_filepath = "./yolo.cfg" weights_filepath = "./yolov3.weights" image_filepath = './image.jpg' meta = pyyolo.load_names(names_filepath) net = pyyolo.load_net(cfg_filepath, weights_filepath, False) im = cv2.imread(image_filepath) yolo_img = pyyolo.array_to_image(im) res = pyyolo.detect(net, meta, yolo_img) colors = np.random.rand(meta.classes, 3) * 255 for r in res: cv2.rectangle( im, r.bbox.get_point(pyyolo.BBox.Location.TOP_LEFT, is_int=True), r.bbox.get_point(pyyolo.BBox.Location.BOTTOM_RIGHT, is_int=True), tuple(colors[r.id].tolist()), 2) cv2.imshow('Frame', im) cv2.waitKey(0)
# camera print('----- test python API using a file') i = 1 while i < 50: start = time.time() ret_val, img_in = cam.read() #img_in = cv2.imread(filename) img = img_in.transpose(2, 0, 1) ch, hh, ww = img.shape[0], img.shape[1], img.shape[2] # print w, h, c data = img.ravel() / 255.0 #data = np.ascontiguousarray(data, dtype=np.float32) data = np.asarray(data, dtype=np.float32, order="c") #ipdb.set_trace() outputs = pyyolo.detect(ww, hh, ch, data, thresh, hier_thresh) #cv2.rectangle(img_in,(0, 0),(1920, 1080), 2, 10) for output in outputs: print(output) #ipdb.set_trace() #cv2.circle(img_in, (output['left'], output['top']), 10, (255,0,0), -1) cv2.rectangle(img_in, (output['left'], output['top']), (output['right'], output['bottom']), (255, 0, 0), 3) i = i + 1 small = cv2.resize(img_in, (0, 0), fx=0.5, fy=0.5) cv2.imshow("video", small) cv2.waitKey(1) dur = time.time() - start print("FPS:%.1f" % (1.0 / dur)) # free model
def worker(camId): pyyolo.init(darknet_path, datacfg, cfgfile, weightfile) CAM_NAME = CAM_CONFIG[camId]['name'] WINDOW_NAME = CAM_CONFIG[camId]['window'] IS_ROTATE = CAM_CONFIG[camId]['rotate'] h = Harvester() h.add_cti_file('/opt/mvIMPACT_Acquire/lib/x86_64/mvGenTLProducer.cti') h.update_device_info_list() try: cam = h.create_image_acquisition_manager(serial_number=CAM_NAME) print("Camera found") except: print("Camera Not Found") exit() cam.start_image_acquisition() lastTime = time.time() transposeTime = 0 i = 0 numberCars = 0 lastSnapshot = None cv2.namedWindow(WINDOW_NAME, flags=0) while (True): buffer = cam.fetch_buffer() image = buffer.payload.components[0].data small = cv2.resize(image, dsize=(320, 200), interpolation=cv2.INTER_CUBIC) clone = small.copy() rgb = cv2.cvtColor(clone, cv2.COLOR_BayerRG2RGB) #img = rgb.transpose(2,0,1) img = np.rot90(rgb) print(rgb.shape) c, h, w = img.shape[0], img.shape[1], img.shape[2] #c, h, w = img.shape[2], img.shape[1], img.shape[0] data = img.ravel() / 255.0 #data = np.ascontiguousarray(data, dtype=np.float32) predictions = pyyolo.detect(w, h, c, data, thresh, hier_thresh) #im = np.zeros((3,small.shape[1],small.shape[0])) #im[0,:,:] = np.rot90(small) #im[1,:,:] = np.rot90(small) #im[2,:,:] = np.rot90(small) #im = rgb print(rgb.shape) #c, h, w = im.shape[0], im.shape[1], im.shape[2] # im = im.transpose(2,0,1) #c, h, w = im.shape[0], im.shape[1], im.shape[2] #c, h, w = 1, image.shape[0], image.shape[1] #im = image.copy() #data = im.ravel()/255.0 #print(data.shape) #data = np.ascontiguousarray(data, dtype=np.float32) #print(data.shape) for output in predictions: left, right, top, bottom, what, prob = output['left'], output[ 'right'], output['top'], output['bottom'], output[ 'class'], output['prob'] #print(output) #lastSnapshot = snapshot.copy() #cv2.imshow("Snapshots", lastSnapshot) if (what == 'car'): print(output) numberCars += 1 cv2.rectangle(rgb, (50, 50), (100, 150), (0, 255, 0), 5) if (camId == "CAM_2"): urllib.request.urlopen(TRIGGER_FAR_FLASH_URL).read() urllib.request.urlopen(TRIGGER_CLOSE_FLASH_URL).read() urllib.request.urlopen(TRIGGER_TRUCK_FLASH_URL).read() if IS_ROTATE: cv2.imshow(WINDOW_NAME, np.rot90(rgb)) else: cv2.imshow(WINDOW_NAME, rgb) cv2.waitKey(1) buffer.queue() print("Count: ", numberCars, " Frame: ", i, " FPS: ", 1.0 / (time.time() - lastTime)) lastTime = time.time() i += 1 cam.stop_image_acquisition() cam.destroy()
def worker(camId): pyyolo.init(darknet_path, datacfg, cfgfile, weightfile) Aravis.enable_interface("Fake") try: cam = Aravis.Camera.new(camId) print("Camera found") except: print("No camera found") exit() thresh = 0.45 hier_thresh = 0.5 #cam.set_region (0,0,2048,1536) #cam.set_region (0,0,512,512) #cam.set_frame_rate (10.0) #cam.set_exposure_time(500000) #cam.set_pixel_format (Aravis.PIXEL_FORMAT_MONO_8) #print(dir(cam)) stream = cam.create_stream(None, None) payload = cam.get_payload() [x, y, width, height] = cam.get_region() print("Camera vendor : %s" % (cam.get_vendor_name())) print("Camera model : %s" % (cam.get_model_name())) print("Camera id : %s" % (cam.get_device_id())) print("ROI : %dx%d at %d,%d" % (width, height, x, y)) print("Payload : %d" % (payload)) print("Pixel format : %s" % (cam.get_pixel_format_as_string())) #cv2.namedWindow('Live Stream', flags=0) cv2.namedWindow(camId, flags=0) #rint(dir(stream)) #for i in range(0,50): #stream.push_buffer (Aravis.Buffer.new_allocate (payload)) cam.start_acquisition() lastTime = time.time() transposeTime = 0 i = 0 while (True): stream.push_buffer(Aravis.Buffer.new_allocate(payload)) buffer = stream.pop_buffer() transposeTime = time.time() b = ctypes.cast(buffer.data, ctypes.POINTER(ctypes.c_uint8)) im = np.ctypeslib.as_array(b, (height, width)) #im = im.copy() #print("shape: ", im.shape) #cv2.imshow("Live Stream", im.copy()) gray = im.copy() cv2.imshow(camId, gray) #im = np.zeros((3,gray.shape[0],gray.shape[1])) #im[1,:,:] = gray #im = cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB) im = np.stack((im, ) * 3, -1) im = im.transpose(2, 0, 1) c, h, w = im.shape[0], im.shape[1], im.shape[2] #print(im.shape) #cv2.imshow(camId, im.copy()) im = im.ravel() / 255.0 #print(im.shape) data = np.ascontiguousarray(im, dtype=np.float32) #print("TRANS-FPS: ", 1.0/(time.time()-transposeTime)) predictions = pyyolo.detect(w, h, c, data, thresh, hier_thresh) # { 'left':0, 'right': 767, 'top': 0, 'bottom': x, class': x, prob: x, } for output in predictions: left, right, top, bottom, what, prob = output['left'], output[ 'right'], output['top'], output['bottom'], output[ 'class'], output['prob'] print(output) if (what == 'car'): d_from_top = top d_from_bottom = bottom d_from_left = left d_from_right = right d_height = top d_width = left if (d_height > 0.5): print(output) #trigger other cameras cv2.waitKey(1) print("CAM: ", camId, " Frame: ", i, " FPS: ", 1.0 / (time.time() - lastTime), "RES: ", h, " x ", w) lastTime = time.time() i += 1 cam.stop_acquisition()
# ret_val = cv2.imwrite(filename,img) # print(ret_val) pyyolo.init(darknet_path, datacfg, cfgfile, weightfile) # from file print('----- test original C using a file') outputs = pyyolo.test(filename, thresh, hier_thresh, 0) for output in outputs: print(output) # camera print('----- test python API using a file') i = 1 while i < 2: # ret_val, img = cam.read() img = cv2.imread(filename) img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5) img = img.transpose(2, 0, 1) c, h, w = img.shape[0], img.shape[1], img.shape[2] # print w, h, c data = img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) outputs = pyyolo.detect(w, h, c, data, thresh, hier_thresh) for output in outputs: print(output) i = i + 1 # free model pyyolo.cleanup()
def get_local_pyyolo_results(img_arr, url='', classes=constants.hls_yolo_categories, method='file'): # from file relevant_bboxes = [] if method == 'file': print('----- test original C using a file') hash = hashlib.sha1() hash.update(str(time.time())) img_filename = hash.hexdigest()[:10] + 'pyyolo.jpg' # img_filename = 'incoming.jpg' cv2.imwrite(img_filename, img_arr) outputs = pyyolo.test(img_filename, thresh, hier_thresh) #not sure what the diff is between this second method (pyyolo.detect) and first (pyyolo.test) #except it uses array instead of file # print('----- test python API using a file') else: i = 1 #wtf is this count for while i < 2: # ret_val, img = cam.read() # img = cv2.imread(filename) img = img_arr.transpose(2, 0, 1) c, h, w = img.shape[0], img.shape[1], img.shape[2] # print w, h, c data = img.ravel() / 255.0 data = np.ascontiguousarray(data, dtype=np.float32) print('calling pyyolo.detect') try: outputs = pyyolo.detect(w, h, c, data, thresh, hier_thresh) except: print('some trouble calling pyyolo detect,' + str(sys.exc_info()[0])) print('returned from pyyolo.detect') for output in outputs: print(output) i = i + 1 # free model for output in outputs: print(output) label = output['class'] if 'person' in label: label = 'person' #convert 'person_wearing_red/blue_shirt' into just person xmin = output['left'] ymin = output['top'] xmax = output['right'] ymax = output['bottom'] conf = output['prob'] item = { 'object': label, 'bbox': [xmin, ymin, xmax, ymax], 'confidence': round(conf, 4) } h, w = img_arr.shape[0:2] frac = 5 cropped_arr = img_arr[h / frac:h - (h / frac), w / frac:w - (w / frac)] dominant_color = imutils.dominant_colors(cropped_arr) print('dominant color:' + str(dominant_color)) if dominant_color is not None: item['details'] = {'color': dominant_color} # item = {'object':label,'bbox':[xmin,ymin,xmax,ymax],'confidence':round(float(confidence),3)} relevant_bboxes.append(item) # pyyolo.cleanup() return relevant_bboxes
#cv2.imshow("Live Stream", im.copy()) gray = im.copy() im = np.zeros((3, gray.shape[0], gray.shape[1])) im[1, :, :] = gray #im = cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB) #im = np.stack((im,)*3,-1) #im = im.transpose(2,0,1) c, h, w = im.shape[0], im.shape[1], im.shape[2] #print(im.shape) #cv2.imshow("Snapshots", im.copy()) im = im.ravel() / 255.0 #print(im.shape) #data = np.ascontiguousarray(im, dtype=np.float32) #print("TRANS-FPS: ", 1.0/(time.time()-transposeTime)) predictions = pyyolo.detect(w, h, c, im, thresh, hier_thresh) cv2.imshow("Livestream", gray.copy()) for output in predictions: left, right, top, bottom, what, prob = output['left'], output[ 'right'], output['top'], output['bottom'], output['class'], output[ 'prob'] #print(output) #lastSnapshot = snapshot.copy() #cv2.imshow("Snapshots", lastSnapshot) if (what == 'car'): d_from_top = top d_from_bottom = bottom d_from_left = left d_from_right = right d_height = top d_width = left
def detector(): # Initialize publisher ROS node pub = rospy.Publisher('predictions', Predictions, queue_size=10) pub1 = rospy.Publisher('peoplecount', Peoplecount, queue_size=10) pub2 = rospy.Publisher('videostream', Image, queue_size=1) rospy.init_node('detector', anonymous=True) # Ceate sort object mot_tracker = Sort() # Define paths for yolo files darknet_path = '/home/ubuntu/catkin_ws/src/jetsontx1_cvmodule/src/pyyolo/darknet' # Only './darknet' is dependent on location of rosrun command datacfg = 'cfg/coco.data' cfgfile = 'cfg/yolov3-tiny.cfg' weightfile = '../yolov3-tiny.weights' #'/media/ubuntu/SDcard/yoloWeights/yolov2-tiny.weights' this also works but it loads way slower filename = darknet_path + '/data/person.jpg' # Image resolution parameters (width, height) = (1280, 720 ) # Use (672,376) for VGA and (1280,720) for HD720 resolution # Initialize visualization fourcc = cv2.VideoWriter_fourcc(*'MJPG') video = cv2.VideoWriter('predictionstest.avi', fourcc, 10, (width, height)) # Initialize pyyolo pyyolo.init(darknet_path, datacfg, cfgfile, weightfile) # Initialize face detector face_cascade = cv2.CascadeClassifier( '/usr/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml') smile_cascade = cv2.CascadeClassifier( '/usr/share/OpenCV/haarcascades/haarcascade_smile.xml') # Create a Camera object zed = sl.Camera() # Create a InitParameters object and set configuration parameters init_params = sl.InitParameters() init_params.camera_resolution = sl.RESOLUTION.HD720 # Use HD1080, HD720 or VGA video mode init_params.camera_fps = 15 # Set fps at 30 # Open the camera err = zed.open(init_params) if err != sl.ERROR_CODE.SUCCESS: exit(1) zed.set_camera_settings(sl.VIDEO_SETTINGS.EXPOSURE, 50) image = sl.Mat() point_cloud = sl.Mat() colours = 255 * np.random.rand(32, 3) # For drawing different colours on BB runtime_parameters = sl.RuntimeParameters() while not rospy.is_shutdown(): start = time.time() # Grab an image, a RuntimeParameters object must be given to grab() if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS: # A new image is available if grab() returns SUCCESS zed.retrieve_image(image, sl.VIEW.LEFT) data = image.get_data() gray_picture = cv2.cvtColor( data, cv2.COLOR_BGR2GRAY ) # Make picture gray for face/smile detection # Retrieve colored point cloud. Point cloud is aligned on the left image. zed.retrieve_measure(point_cloud, sl.MEASURE.XYZRGBA) Data = data.transpose(2, 0, 1) start5 = time.time() Data = Data.ravel() / 255.0 end5 = time.time() Data = np.ascontiguousarray(Data, dtype=np.float32) start1 = time.time() outputs = pyyolo.detect( width, height, 4, Data, 0.5, 0.8 ) #pyyolo.detect returns: [{'right':, 'bottom':, 'top':, 'class':, 'prob':, 'left':}] end1 = time.time() print("Section cycle time: ", end1 - start1) dets = np.empty((0, 5), int) count = 0 for output in outputs: if output['class'] == 'person': #track only people count = count + 1 # Count detected people dets = np.append(dets, [[ output['left'], output['top'], output['right'], output['bottom'], output['prob'] ]], axis=0) people = Peoplecount() people.tot_detected_people = count pub1.publish(people) preds = Predictions() trackers = mot_tracker.update( dets ) #update tracking ID. mot_tracker.update() returns: [[x1,y1,x2,y2,id]] for d in trackers: d = d.astype(np.int32) # Create a dictionary to store info for publishing detectinfo = { 'left': d[0], 'top': d[1], 'right': d[2], 'bottom': d[3], 'class': 'person', 'ID': int(d[4]) } euclidean_distance(detectinfo, point_cloud) relative_coordinates(detectinfo, width) start3 = time.time() facesmile_detect(detectinfo, gray_picture, data, face_cascade, smile_cascade) end3 = time.time() #print("facesmile detect cycle time: ", end3 - start3) #print detectinfo['smile'] pred = Prediction() #pred.probabilities.append() pred.classes.append(detectinfo['class']) pred.xmin = detectinfo['left'] pred.ymin = detectinfo['top'] pred.xmax = detectinfo['right'] pred.ymax = detectinfo['bottom'] pred.id = detectinfo['ID'] pred.face = detectinfo['face'] pred.smile = detectinfo['smile'] pred.distance = detectinfo['distance'] pred.angle = detectinfo['angle'] pred.xcoord = detectinfo['x'] pred.ycoord = detectinfo['y'] preds.predictions.append(pred) label = detectinfo['class'] + " " + str(detectinfo['ID']) cv2.rectangle(data, (d[0], d[1]), (d[2], d[3]), (colours[d[4] % 32, 0], colours[d[4] % 32, 1], colours[d[4] % 32, 2]), 2) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(data, label, (d[2], d[1] + 25), font, 1, (0, 0, 255), 1, cv2.LINE_AA) pub.publish(preds) msg_frame = CvBridge().cv2_to_imgmsg(data, "8UC4") pub2.publish(msg_frame) #video.write(data[:,:,:3])#because data.shape is (376,672,4) and it only supports 3 channels. cv2.imshow("image", data) cv2.waitKey(35) end = time.time() print("Total cycle time: ", end - start) # Close the camera zed.close()
cam = cv2.VideoCapture(-1) # cam = cv2.VideoCapture("http://192.168.1.23:4747/video") outputs = [] pyyolo.init(darknet_path, datacfg, cfgfile, weightfile) while True: frame += 1 _, img = cam.read() if frame % 4 == 0: height, width, channels = img.shape[:3] transposed = img.transpose(2, 0, 1) # move channels to beginning data = transposed.ravel() / 255.0 # linearize and normalize data = np.ascontiguousarray(data, dtype=np.float32) outputs = pyyolo.detect(width, height, channels, data, thresh, hier_thresh) color = (255, 255, 255) for output in outputs: if output["prob"] >= 0.2: color = list(np.random.random(size=3) * 256) tl = (output["left"], output["top"]) cv2.rectangle(img, tl, (output["right"], output["bottom"]), color) cv2.putText(img, "{} ({:.2f} %)".format(output["class"], output["prob"] * 100), (tl[0] - 20, tl[1] - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.0, color) cv2.imshow("win", img) key = cv2.waitKey(delay) & 0xFF pyyolo.cleanup()