人臉口罩檢測(含執行程式碼+資料集)Pytorch+TensorRT+Xavier NX

專注的阿熊發表於2023-01-17

#14

def do_nms(det, boxes, confs, clss):

     drop = False

     if len(boxes) <= 0:

         boxes.append((det[0],det[1],det[2],det[3]))

         confs.append(det[4])

         clss.append(det[5])

         return boxes, confs, clss

     for i in range(0,len(boxes)):

         bbox = boxes[i]

         xx1 = np.maximum(det[0], bbox[0])

         yy1 = np.maximum(det[1], bbox[1])

         xx2 = np.minimum(det[2], bbox[2])

         yy2 = np.minimum(det[3], bbox[3])

         w = np.maximum(0.0, xx2-xx1+1)

         h = np.maximum(0.0, yy2-yy1+1)

         area_det = (det[2]-det[0]+1)*(det[3]-det[1]+1)

         area_bbox = (bbox[2]-bbox[0]+1)*(bbox[3]-bbox[1]+1)

         inter = w*h

         ovr = inter / (area_det + area_bbox - inter)

         if ovr > 0.6 and not drop:

             if det[4] > confs[i]:

                 boxes[i] = ((det[0],det[1],det[2],det[3]))

                 confs[i] = det[4]

                 clss[i] = det[5]

             drop = True

     if not drop:

         boxes.append((det[0],det[1],det[2],det[3]))

         confs.append(det[4])

         clss.append(det[5])

     return boxes, confs, clss

def _preprocess_trt(img, shape=(300, 300)):

     """Preprocess an image before TRT SSD inferencing."""

     img = cv2.resize(img, shape)

     img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

     img = img.transpose((2, 0, 1)).astype(np.float32)

     img *= (2.0/255.0)

     img -= 1.0

     return img

def _postprocess_trt(img, output, conf_th, output_layout):

     """Postprocess TRT SSD output."""

     img_h, img_w, _ = img.shape

     boxes, confs, clss, results = [], [], [],[]

     #print(((len(output[1]))/4+1))

     #print("len(outputs[0]): "+str(len(output[0]))+" len(outputs[1]): "+str(len(output[1])))

     for n in range(0, int((len(output[1]))/4)):

         maxScore =跟單網gendan5.com -1000.0000

         maxClass = 0

         for m in range(0, 4):

             score = output[0][n*4+m]

             #print(score)

             if score < conf_th:

                 continue

             if m <= 0:

                 continue

             if( score > maxScore):

                 maxScore = score

                 maxClass = m

         #if(maxClass < 0):

         #    continue

         index = int(n)

         if maxScore < conf_th:

             continue

         #print(str(output[1][n*4+0])+" "+str(output[1][n*4+1])+" "+str(output[1][n*4+2])+" "+str(output[1][n*4+3]))

         x1 = int(output[1][n*4+0] * img_w)

         y1 = int(output[1][n*4+1] * img_h)

         x2 = int(output[1][n*4+2] * img_w)

         y2 = int(output[1][n*4+3] * img_h)

         det = [x1,y1,x2,y2,maxScore,maxClass,n]

         boxes, confs, clss = do_nms(det, boxes, confs, clss)

     return boxes, confs, clss


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2932460/,如需轉載,請註明出處,否則將追究法律責任。

相關文章