def writeBoxes(params=[], detObjs=[], detObjFileHndl='', writeBboxMap=[], labelmap=[], lblMapHashBased=False): for i in range(detObjs.shape[0]): label = int(detObjs[i][4]) score = detObjs[i][5] age = detObjs[i][6] strng_trk = detObjs[i][7] label_name = str( get_labelname(labelmap, label, lblMapHashBased=lblMapHashBased)[0]) writeThisBox = True #if age is young then choose only if score is high if (age <= 3) and (strng_trk == False) and params.enObjPropExp: writeThisBox = False if writeThisBox: writeOneBox(enable=params.writeBbox, bbox=detObjs[i], label_name=label_name, score=score, fileHndl=detObjFileHndl, writeBboxMap=writeBboxMap, age=age, strng_trk=strng_trk) return
def drawBoxes(params=[], detObjs=[], drawHandle=[], writeBboxMap=[], labelmap=[], lblMapHashBased=False): #FIX_ME:SN. Propagate_obj.py also defines. Unify. STRNG_TRK_IDX = 7 colors = plt.cm.hsv(np.linspace(0, 1, 255)).tolist() for idx in range(detObjs.shape[0]): label = int(detObjs[idx][4]) score = detObjs[idx][5] age = detObjs[idx][6] strng_trk = detObjs[idx][STRNG_TRK_IDX] label_name = str( get_labelname(labelmap, label, lblMapHashBased=lblMapHashBased)[0]) if type(params.confTh) is dict: draw_cur_reg = score > params.confTh[label_name] else: draw_cur_reg = score > params.confTh #if age is young then choose only if score is high if (age <= 3) and params.enObjPropExp: draw_cur_reg = (strng_trk == 1) if draw_cur_reg: display_txt = '%.3s: %.2f' % (label_name[:4], score) if label > 254: print(label) color = colors[label] #/usr/share/fonts/truetype/droid/DroidSans.ttf fnt = ImageFont.truetype("DroidSans.ttf", 10) #display score and label name cor = detObjs[idx] drawHandle.text((cor[0] + REC_WIDTH, cor[1] + REC_WIDTH), display_txt, (255, 255, 255), font=fnt) # determine what color rect to draw rectColorAry = [ "violate", "blue", "yellow", "orange", "red", "pink", "white", "brown", "green" ] numColor = len(rectColorAry) label = min(label, numColor - 1) rectColor = rectColorAry[label] #draw.rectangle(((xmin,ymin),(xmax,ymax)), outline = "blue") # hack to draw rectangle with larger than 1 px thickness. As ImageDraw natively does not support it. for i in range(-REC_WIDTH, REC_WIDTH): locRec = (cor[0] + i, cor[1] + i, cor[2] + i, cor[3] + i) drawHandle.rectangle(locRec, outline=rectColor) return drawHandle
def processOneCrop(curScaleImage, transformer, net, drawHandle, detBBoxesCurFrame, offsetX, offsetY, scaleX, scaleY, aspectRatio, confTh, externalDet=False, extDetFileName='', labelmap=''): if externalDet == False: transformed_image = transformer.preprocess('data', curScaleImage) net.blobs['data'].data[...] = transformed_image # Forward pass. detections = net.forward()['detection_out'] else: print("read detections from file") detections = readDetsFromFile(extDetFileName=extDetFileName, offsetX=offsetX, offsetY=offsetY, scaleX=scaleX, scaleY=scaleY, curScaleImage=curScaleImage) #use haskey based labelmap when external det is enabled lblMapHashBased = externalDet #print("detections.shape", detections.shape) #print("detections.dtype", detections.dtype) #print("detections[0]", detections[0,0,0,:]) # Parse the outputs. det_label = detections[0, 0, :, 1] det_conf = detections[0, 0, :, 2] det_xmin = detections[0, 0, :, 3] det_ymin = detections[0, 0, :, 4] det_xmax = detections[0, 0, :, 5] det_ymax = detections[0, 0, :, 6] # Get detections with confidence higher than confTh(def =0.6). det_label_list = det_label.astype(np.int).tolist() #indiates age of the tracked obj. In the frame it gets detected (born) set it to 0 age = 0.0 #indicates whether current object is part of strog track or not. Gets used #by ObjProp strng_trk = 0 if type(confTh) is dict: confThList = [None] * len(det_label_list) for i, det_label_cur_obj in enumerate(det_label_list): if (det_label_cur_obj <> -1): confThList[i] = confTh[str( get_labelname(labelmap, det_label_cur_obj, lblMapHashBased=lblMapHashBased)[0])] else: #some thing went wrong. Set conservative th confThList[i] = 1.0 top_indices = [ i for i, conf in enumerate(det_conf) if (conf > confThList[i]) ] else: top_indices = [i for i, conf in enumerate(det_conf) if conf >= confTh] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_labels = get_labelname(labelmap, top_label_indices, lblMapHashBased=lblMapHashBased) top_xmin = det_xmin[top_indices] top_ymin = det_ymin[top_indices] top_xmax = det_xmax[top_indices] top_ymax = det_ymax[top_indices] colors = plt.cm.hsv(np.linspace(0, 1, 255)).tolist() for i in xrange(top_conf.shape[0]): xmin = top_xmin[i] * curScaleImage.shape[1] ymin = top_ymin[i] * curScaleImage.shape[0] xmax = top_xmax[i] * curScaleImage.shape[1] ymax = top_ymax[i] * curScaleImage.shape[0] score = top_conf[i] label = int(top_label_indices[i]) label_name = top_labels[i] if label > 254: print(label) color = colors[label] #display score and label name #print "xmin, ymin, xmax, ymax", xmin, " , ", ymin," , ", xmax," , ", ymax #print "scaleX:scaleY ", scaleX, " , ", scaleY #print "offsetX:Y ", offsetX, " , ", offsetY bbox = (int(round(xmin * scaleX)) + offsetX, int(round(ymin * scaleY)) + offsetY, int(round(xmax * scaleX)) + offsetX, int(round(ymax * scaleY)) + offsetY, label, score, age, strng_trk) #print "bbox : ", bbox # store box co-ordinates along with label and score detBBoxesCurFrame.append(bbox) return [drawHandle, detections]