Esempio n. 1
0
    def detect_image(self, image):
        if self.is_fixed_size:
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        #print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.
        
        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        return_boxs = []
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            if predicted_class != 'person' :
                continue
            box = out_boxes[i]
           # score = out_scores[i]  
            x = int(box[1])  
            y = int(box[0])  
            w = int(box[3]-box[1])
            h = int(box[2]-box[0])
            if x < 0 :
                w = w + x
                x = 0
            if y < 0 :
                h = h + y
                y = 0 
            return_boxs.append([x,y,w,h])

        return return_boxs
Esempio n. 2
0
    def detect_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                    size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        print(end - start)
        return image
Esempio n. 3
0
    def detect_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)

        image_data = np.array(boxed_image, dtype='float32')

        # print(" image_data.shape:",image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            # print(label, (left, top), (right, bottom))
            center_y = (top + bottom) / 2
            center_x = (left + right) / 2
            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
                draw.point((center_x, center_y), fill=(255, 0, 0))
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        # print("时间:",end - start)
        return image
Esempio n. 4
0
    def detect_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):                                   #设置检测图片的大小,即输入神经网络的图片大小
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'     #可以和实际大小不一致,但必须是32的倍数
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))    #letterbox_image返回根据model_image_size缩放大小的图片
        else:                                                                       #如果不设置model_image_size,则去掉32的余数之后,按照实际大小输入神经网络
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)                    #返回按照实际尺寸去掉32余数之后的图片大小
        image_data = np.array(boxed_image, dtype='float32')

        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.
        print('sess run before',self.input_image_shape)
        print(image_data.shape, image.size)
        out_boxes, out_scores, out_classes = self.sess.run(                         #out_boxes是所有检测框的坐标,out_scores是该检测框的得分,out_classes是检测到的类别
            [self.boxes, self.scores, self.classes],
            feed_dict={                                                             #feed_dict表示临时生效
                self.yolo_model.input: image_data,                                  #image_data是经过参数model_image_size缩放了的图片数据
                self.input_image_shape: [image.size[1], image.size[0]],             #image.size是原始图片大小数据
                K.learning_phase(): 0                                               #0表示训练模式,1表示测试模式
            })
        print('sess run after',self.input_image_shape)
        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                    size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300
        led_result =  open('.\\logs\\led_result.txt', 'w')

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c] + str(i+1)
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left,top),(right,bottom))
            led_result.write(str(label) + '/' + str(left) + ',' + str(top) + ',' + str(right) + ',' + str(bottom) + '\n')

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        print('The time is:',end - start)
        led_result.close()
        return image
    def detect_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
            
        file_name = '_results.csv'        
        csvFile = open(file_name, 'w',newline = '')
        writer = csv.writer(csvFile)

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                    size=np.floor((3e-2 * image.size[1] + 0.5)*0.8).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))
            infor = [predicted_class, score, left, top, right, bottom]   
            writer.writerow(infor)
        
            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
            #draw.rectangle(
            #   [tuple(text_origin), tuple(text_origin + label_size)],
            #   fill=self.colors[c])
            #draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            #del draw
            # Do not show the labels!
        end = timer()
        print(end - start)
        return image
Esempio n. 6
0
    def detect_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size))) #letterbox()标准化尺寸??
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        
        global num_cone
        num_cone=len(out_boxes)

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                    size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            global Class
            Class=c
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            #calculate distance to the cones
            global locX,locY
            locX=round((left+right)/2)
            locY=round(top+(bottom-top)*0.8)

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.ellipse(((left+right)/2,(top+(bottom-top)*0.8) , 5, 5), fill=self.colors[c])
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self.colors[c])

            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        print(end - start)
        #import cv2
        #cv2.imshow("detected",image)
        return image
Esempio n. 7
0
    def detect_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        # print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        #将右下坐标转换成w h
        # out_boxes[0][2] = out_boxes[0][2] - out_boxes[0][0]
        # out_boxes[0][3]=out_boxes[0][3]-out_boxes[0][1]
        # print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
        #
        # font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
        #             size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        # thickness = (image.size[0] + image.size[1]) // 300
        #
        # for i, c in reversed(list(enumerate(out_classes))):
        #     predicted_class = self.class_names[c]
        #     box = out_boxes[i]
        #     score = out_scores[i]
        #
        #     label = '{} {:.2f}'.format(predicted_class, score)
        #     draw = ImageDraw.Draw(image)
        #     label_size = draw.textsize(label, font)
        #
        #     top, left, bottom, right = box
        #     top = max(0, np.floor(top + 0.5).astype('int32'))
        #     left = max(0, np.floor(left + 0.5).astype('int32'))
        #     bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
        #     right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
        #     print(label, (left, top), (right, bottom))
        #
        #     if top - label_size[1] >= 0:
        #         text_origin = np.array([left, top - label_size[1]])
        #     else:
        #         text_origin = np.array([left, top + 1])
        #
        #     # My kingdom for a good redistributable image drawing library.
        #     for i in range(thickness):
        #         draw.rectangle(
        #             [left + i, top + i, right - i, bottom - i],
        #             outline=self.colors[c])
        #     draw.rectangle(
        #         [tuple(text_origin), tuple(text_origin + label_size)],
        #         fill=self.colors[c])
        #     draw.text(text_origin, label, fill=(0, 0, 0), font=font)
        #     del draw

        # end = timer()
        # print(end - start)
        # return image
        return out_boxes
Esempio n. 8
0
    def detect_image(self, image):
        start = timer()  # 开始计时

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)  # 打印图片的尺寸
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes),
                                             'img'))  # 提示用于找到几个bbox

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(2e-2 * image.size[1] +
                                                0.2).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 500

        # 保存框检测出的框的个数
        # file.write('find  ' + str(len(out_boxes)) + ' target(s) \n')

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            font = ImageFont.truetype("simsun.ttc", 20, encoding='unic')
            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))

            # 写入检测位置
            # file.write(
            #     predicted_class + '  score: ' + str(score) + ' \nlocation: top: ' + str(top) + '、 bottom: ' + str(
            #         bottom) + '、 left: ' + str(left) + '、 right: ' + str(right) + '\n')
            file.write(' ' + str(self.class_names.index(predicted_class) + 1) +
                       ' ' + str(score) + ' ' + str(left) + ' ' + str(top) +
                       ' ' + str(right) + ' ' + str(bottom) + ';')

            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            # font = ImageFont.truetype("simsun.ttc", 20, encoding='unic')
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        print('time consume:%.3f s ' % (end - start))
        return image
Esempio n. 9
0
    def detect_image(self, image, csv_list):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        # print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        if len(out_scores) == 0:
            return None, None
        i = 0
        c = out_classes[0]
        print(out_scores)
        # print(i)
        predicted_class = self.class_names[c]
        box = out_boxes[i]
        score = out_scores[i]

        label = '{} {:.2f}'.format(predicted_class, score)
        draw = ImageDraw.Draw(image)
        label_size = draw.textsize(label, font)

        top, left, bottom, right = box
        top = max(0, np.floor(top + 0.5).astype('int32'))
        left = max(0, np.floor(left + 0.5).astype('int32'))
        bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
        right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
        print(label, (left, top), (right, bottom))

        # add csv data written by nakatani
        csv_list.append(label.split()[1])

        # socket block written by nakatani
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            # サーバを指定
            s.connect(('127.0.0.1', 90))
            # サーバにメッセージを送る
            # s.sendall(b'hello')
            send_message = label + ":"
            s.sendall(send_message.encode("UTF-8"))

        if top - label_size[1] >= 0:
            text_origin = np.array([left, top - label_size[1]])
        else:
            text_origin = np.array([left, top + 1])

        # My kingdom for a good redistributable image drawing library.
        image = image.crop((left, top, right, bottom))

        # show bounding box 171 - 201 written by nakatani
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            # print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        print(end - start, "seconds takes")
        return image, csv_list
    def detect_image_pic(self, image):  #检测每一张图片的人脸位置
        start = timer()  # 起始时间
        pic_filename = os.path.basename(img_path)
        # txt_filename=pic_filename.replace("jpg","txt")
        portion = os.path.splitext(pic_filename)
        if portion[1] == '.jpg':
            txt_result = predict_result + portion[0] + '.txt'
        print('txt_result的路径是:' + txt_result)
        if self.model_image_size != (
                None, None):  # 416x416, 416=32*13,必须为32的倍数,最小尺度是除以32
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image,
                                          tuple(reversed(
                                              self.model_image_size)))  # 填充图像
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')
        print('detector size {}'.format(image_data.shape))
        image_data /= 255.  # 转换0~1
        image_data = np.expand_dims(image_data, 0)  # 添加批次维度,将图片增加1维

        # 参数盒子、得分、类别;输入图像0~1,4维;原始图像的尺寸
        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))  # 检测出的框

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))  # 字体
        thickness = (image.size[0] + image.size[1]) // 128  # 厚度
        predicted_class_list = []
        box_list = []  # 用来存储坐标位置
        score_list = []  # 用来存储置信值
        with open(txt_result, 'a') as new_f:
            for i, c in reversed(list(enumerate(out_classes))):
                predicted_class = self.class_names[c]  # 类别
                box = out_boxes[i]  # 框
                score = out_scores[i]  # 执行度

                label = '{} {:.2f}'.format(predicted_class, score)  # 标签,是预测概率值
                draw = ImageDraw.Draw(image)  # 画图
                label_size = draw.textsize(label, font)  # 标签文字

                top, left, bottom, right = box
                top = max(0, np.floor(top + 0.5).astype('int32'))
                left = max(0, np.floor(left + 0.5).astype('int32'))
                bottom = min(image.size[1],
                             np.floor(bottom + 0.5).astype('int32'))
                right = min(image.size[0],
                            np.floor(right + 0.5).astype('int32'))
                print(
                    label, (left, top),
                    (right,
                     bottom))  # 边框,这个就是【置信值,xmin,ymin,xmax,ymax】,可以做一下mAP值的分析了
                predicted_class_list.append(predicted_class)
                box_list.append([left, top, right, bottom])
                score_list.append(score)

                if top - label_size[1] >= 0:  # 标签文字
                    text_origin = np.array([left, top - label_size[1]])
                else:
                    text_origin = np.array([left, top + 1])

            if len(score_list) == 0:
                return None
            # 获取最大的置信值
            max_index = score_list.index(max(score_list))
            max_score = score_list[max_index]
            max_predicted_class = predicted_class_list[max_index]
            max_box = box_list[max_index]
            max_left, max_top, max_right, max_bottom = max_box
            print("更新之后的坐标标签是:", (max_left, max_top), (max_right, max_bottom))
            label = '{} {:.2f}'.format(max_predicted_class, max_score)  # 标签
            # 这里需要改
            new_f.write(
                str(label) + " " + str(max_left) + " " + str(max_top) + " " +
                str(max_right) + " " + str(max_bottom) + '\n')
            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):  # 画框
                draw.rectangle(
                    [max_left + i, max_top + i, max_right - i, max_bottom - i],
                    outline=self.colors[c])
            draw.rectangle(  # 文字背景是红色
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0),
                      font=font)  # 文字内容,face+是人脸的概率值
            del draw

        end = timer()
        print(end - start)  # 检测执行时间
        return image
Esempio n. 11
0
    def eval(self,
             step,
             eval_images_path,
             ground_truth_path,
             tag='image',
             is_save_images=True):
        # Add the class predict temp dict
        class_pred_tmp = {}
        for class_name in self.class_names:
            class_pred_tmp[class_name] = []

        # Predict!!!
        for start in range(0, len(eval_images_path), self.step2_batch_size):
            end = start + self.step2_batch_size
            images_path = eval_images_path[start:end]
            images = []
            images_org = []
            images_shape = []
            files_id = []
            for image_path in images_path:
                image = Image.open(image_path)
                file_id = os.path.split(image_path)[-1].split('.')[0]
                boxed_image = letterbox_image(
                    image, tuple(reversed(self.input_shape)))
                image_data = np.array(boxed_image, dtype='float32')
                image_data /= 255.
                images_shape.append([image.size[1], image.size[0]])
                images.append(image_data)
                images_org.append(image)
                files_id.append(file_id)
            images = np.array(images)

            out_bboxes_1, out_bboxes_2, out_bboxes_3 = self.yolo_body.predict_on_batch(
                images)
            for i, out in enumerate(
                    zip(out_bboxes_1, out_bboxes_2, out_bboxes_3)):
                # Predict
                out_boxes, out_scores, out_classes = self.sess.run(
                    [self.boxes, self.scores, self.classes],
                    feed_dict={
                        # self.eval_inputs: out
                        self.eval_inputs[0]:
                        np.expand_dims(out[0], 0),
                        self.eval_inputs[1]:
                        np.expand_dims(out[1], 0),
                        self.eval_inputs[2]:
                        np.expand_dims(out[2], 0),
                        self.input_image_shape:
                        images_shape[i]
                    })

                image = np.array(images_org[i])
                ord_h = image.shape[0]
                ord_w = image.shape[1]
                new_h = int(image.shape[0] * 3 / 4)
                new_w = int(image.shape[1] * 3 / 4)
                image = cv2.resize(image, (new_w, new_h))
                for o, c in enumerate(out_classes):
                    predicted_class = self.class_names[c]
                    box = out_boxes[o]
                    score = out_scores[o]

                    top, left, bottom, right = box
                    top = max(0, np.floor(top + 0.5).astype('int32'))
                    left = max(0, np.floor(left + 0.5).astype('int32'))
                    bottom = min(ord_h, np.floor(bottom + 0.5).astype('int32'))
                    right = min(ord_w, np.floor(right + 0.5).astype('int32'))

                    bbox = "{} {} {} {}".format(left, top, right, bottom)
                    class_pred_tmp[predicted_class].append({
                        "confidence":
                        str(score),
                        "file_id":
                        files_id[i],
                        "bbox":
                        bbox
                    })

                    # Draw image
                    new_top, new_left, new_bottom, new_right = (
                        box * 3 / 4).astype('int32')
                    if files_id[i] in self.eval_save_images_id:
                        label = '{} {:.2f}'.format(predicted_class, score)
                        # print(label, (left, top), (right, bottom))
                        color = self.colors[c]
                        cv2.rectangle(image, (new_left, new_top),
                                      (new_right, new_bottom), color, 2)
                        font_size = math.sqrt((new_right - new_left) *
                                              (new_bottom - new_top)) / 50
                        if font_size > 0.8:
                            font_size = 0.8
                        elif font_size < 0.3:
                            font_size = 0.3
                        cv2.putText(image, label, (new_left, new_top - 3),
                                    cv2.FONT_HERSHEY_SIMPLEX, font_size, color,
                                    1)
                if is_save_images:
                    if files_id[i] in self.eval_save_images_id:
                        log_images(self.callback, tag + '/' + files_id[i],
                                   [image], step)

        # Create predict temp
        for class_name in self.class_names:
            with open(
                    self.tmp_pred_files_path + "/" + class_name +
                    "_predictions.json", 'w') as outfile:
                json.dump(class_pred_tmp[class_name], outfile)

        # calculate the AP for each class
        sum_AP = 0.0
        count_true_positives = {}
        for class_index, class_name in enumerate(
                sorted(self.gt_counter_per_class.keys())):
            count_true_positives[class_name] = 0

            # load predictions of that class
            predictions_file = self.tmp_pred_files_path + "/" + class_name + "_predictions.json"
            predictions_data = json.load(open(predictions_file))

            # Assign predictions to ground truth objects
            nd = len(predictions_data)  # number of predict data
            tp = [0] * nd  # true positive
            fp = [0] * nd  # false positive
            for idx, prediction in enumerate(predictions_data):
                file_id = prediction["file_id"]
                gt_file = ground_truth_path + "/" + file_id + "_ground_truth.json"
                ground_truth_data = json.load(open(gt_file))
                ovmax = -1
                gt_match = -1
                # load prediction bounding-box
                bb = [float(x) for x in prediction["bbox"].split()]
                for obj in ground_truth_data:
                    # look for a class_name match
                    if obj["class_name"] == class_name:
                        bbgt = [float(x) for x in obj["bbox"].split()]
                        # Area of Overlap
                        bi = [
                            max(bb[0], bbgt[0]),
                            max(bb[1], bbgt[1]),
                            min(bb[2], bbgt[2]),
                            min(bb[3], bbgt[3])
                        ]
                        iw = bi[2] - bi[0] + 1
                        ih = bi[3] - bi[1] + 1
                        # compute overlap (IoU) = area of intersection / area of union
                        if iw > 0 and ih > 0:
                            # Area of Union
                            ua = (bb[2] - bb[0] + 1) * (bb[3] - bb[1] + 1) + \
                                 (bbgt[2] - bbgt[0] + 1) * (bbgt[3] - bbgt[1] + 1) - iw * ih
                            ov = iw * ih / ua
                            if ov > ovmax:
                                ovmax = ov
                                gt_match = obj

                if ovmax >= self.min_overlap:
                    if not gt_match['used']:
                        tp[idx] = 1
                        gt_match["used"] = True
                        # count_true_positives[predicted_class] += 1
                        with open(gt_file, 'w') as f:
                            f.write(json.dumps(ground_truth_data))
                    else:
                        fp[idx] = 1
                else:
                    fp[idx] = 1

            # compute precision/recall
            cumsum = 0
            for idx, val in enumerate(fp):
                fp[idx] += cumsum
                cumsum += val
            cumsum = 0
            for idx, val in enumerate(tp):
                tp[idx] += cumsum
                cumsum += val
            rec = tp[:]
            for idx, val in enumerate(tp):
                rec[idx] = float(
                    tp[idx]) / self.gt_counter_per_class[class_name]
            prec = tp[:]
            for idx, val in enumerate(tp):
                prec[idx] = float(tp[idx]) / (fp[idx] + tp[idx])

            ap, mrec, mprec = self.voc_ap(rec, prec)
            sum_AP += ap

            log_scalar(self.callback, tag + '_mAP/' + class_name, ap * 100,
                       step)
        mAP = sum_AP / len(self.gt_counter_per_class)
        log_scalar(self.callback, 'Total_mAP/' + tag, mAP * 100, step)

        # remove the tmp_files directory
        shutil.rmtree(self.tmp_pred_files_path)
        shutil.rmtree(ground_truth_path)
        os.mkdir(self.tmp_pred_files_path)
        shutil.copytree(ground_truth_path + '_org', ground_truth_path)
        return mAP * 100
Esempio n. 12
0
    def detect_image(self,
                     image,
                     single_image=True,
                     output=list(),
                     bbox_set=list(),
                     label_set=list(),
                     score_set=list(),
                     speed=list()):

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        if single_image: print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        start = timer()
        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        end = timer()

        if single_image:
            print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
        # Label & Score font Size.
        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(6e-2 * image.size[1] +
                                                0.5).astype('int32'))

        # Bounding Box thickness.
        thickness = max(1, (image.size[0] + image.size[1]) // 300)

        for i, c in (list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            old_top, old_left, old_bottom, old_right = box
            top = max(0, np.floor(old_top + 0.5).astype('int32'))
            left = max(0, np.floor(old_left + 0.5).astype('int32'))
            bottom = min(image.size[1],
                         np.floor(old_bottom + 0.5).astype('int32'))
            right = min(image.size[0],
                        np.floor(old_right + 0.5).astype('int32'))
            if single_image:
                print(label, (old_left, old_top), (old_right, old_bottom))
            else:
                y1 = int(round(old_top))
                x1 = int(round(old_left))
                y2 = int(round(old_bottom))
                x2 = int(round(old_right))
                output_str = '{} {} {} {} {}'.format(label, y1, x1, y2, x2)
                output.append(output_str)

                bbox_set.append(([y1, x1, y2, x2]))
                label_set.append(int(predicted_class))
                score_set.append(score)

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        #end = timer()
        print(end - start)
        speed.append(float(end - start))
        if single_image: print(end - start)
        return image
Esempio n. 13
0
    def generate(self, FLAGS):
        model_path = os.path.expanduser(FLAGS['model'])
        assert model_path.endswith(
            '.h5'), 'Keras model or weights must be a .h5 file.'

        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        try:
            model = tf.keras.models.load_model(model_path, compile=False)
        except:
            if self.backbone == BACKBONE.MOBILENETV2:
                model_body = partial(mobilenetv2_yolo_body,
                                     alpha=FLAGS['alpha'])
            elif self.backbone == BACKBONE.DARKNET53:
                model_body = darknet_yolo_body
            elif self.backbone == BACKBONE.EFFICIENTNET:
                model_body = partial(efficientnet_yolo_body,
                                     model_name='efficientnet-b4')
            if tf.executing_eagerly():
                model = model_body(tf.keras.layers.Input(
                    shape=(*self.input_shape, 3), name='predict_image'),
                                   num_anchors=num_anchors // 3,
                                   num_classes=num_classes)
            else:
                self.input = tf.keras.layers.Input(shape=(None, None, 3),
                                                   name='predict_image',
                                                   dtype=tf.uint8)
                input = tf.map_fn(
                    lambda image: tf.image.convert_image_dtype(
                        image, tf.float32), self.input, tf.float32)
                image, shape = letterbox_image(input, self.input_shape)
                self.input_image_shape = tf.shape(input)[1:3]
                image = tf.reshape(image, [-1, *self.input_shape, 3])
                model = model_body(image,
                                   num_anchors=num_anchors // 3,
                                   num_classes=num_classes)
            model.load_weights(
                model_path)  # make sure model, anchors and classes match
        else:
            assert model.layers[-1].output_shape[-1] == \
                   num_anchors / len(model.output) * (num_classes + 5), \
                'Mismatch between model and given anchor and class sizes'
        print('{} model, anchors, and classes loaded.'.format(model_path))
        # Generate colors for drawing bounding boxes.
        if tf.executing_eagerly():
            self.yolo_model = model
        else:
            output = YoloEval(self.anchors,
                              len(self.class_names),
                              self.input_image_shape,
                              score_threshold=self.score,
                              iou_threshold=self.nms,
                              name='yolo')(model.output)
            # output = tf.keras.layers.Lambda(lambda input: yolo_eval(
            #     input,
            #     self.anchors,
            #     len(self.class_names),
            #     self.input_image_shape,
            #     score_threshold=self.score,
            #     iou_threshold=self.nms),name='yolo')(model.output)
            self.yolo_model = tf.keras.Model(model.input, output)
        # Generate output tensor targets for filtered bounding boxes.
        hsv_tuples: List[Tuple[float, float, float]] = [
            (x / len(self.class_names), 1., 1.)
            for x in range(len(self.class_names))
        ]
        self.colors: List[Tuple[float, float, float]] = list(
            map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors: List[Tuple[int, int, int]] = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        np.random.seed(10101)  # Fixed seed for consistent colors across runs.
        np.random.shuffle(
            self.colors)  # Shuffle colors to decorrelate adjacent classes.
        np.random.seed(None)  # Reset seed to default.
Esempio n. 14
0
    def detect_image(self, image, draw=True) -> Image:
        if tf.executing_eagerly():
            image_data = tf.expand_dims(image, 0)
            if self.input_shape != (None, None):
                boxed_image, image_shape = letterbox_image(
                    image_data, tuple(reversed(self.input_shape)))
            else:
                height, width, _ = image_data.shape
                new_image_size = (width - (width % 32), height - (height % 32))
                boxed_image, image_shape = letterbox_image(
                    image_data, new_image_size)
            image_data = np.array(boxed_image)
            start = timer()
            output = self.yolo_model.predict(image_data)
            out_boxes, out_scores, out_classes = yolo_eval(
                output,
                self.anchors,
                len(self.class_names),
                image.shape[0:2],
                score_threshold=self.score,
                iou_threshold=self.nms)
            end = timer()
            image = Image.fromarray((np.array(image) * 255).astype('uint8'),
                                    'RGB')
        else:
            image_data = np.expand_dims(image, 0)
            start = timer()
            out_boxes, out_scores, out_classes = self.sess.run(
                self.yolo_model.output, feed_dict={self.input: image_data})
            end = timer()

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
        if draw:
            font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                      size=np.floor(3e-2 * image.size[1] +
                                                    0.5).astype('int32'))
            thickness = (image.size[1] + image.size[0]) // 300
            draw = ImageDraw.Draw(image)
            for i, c in reversed(list(enumerate(out_classes))):
                predicted_class = self.class_names[c]
                box = out_boxes[i]
                score = out_scores[i]

                label = '{} {:.2f}'.format(predicted_class, score)

                label_size = draw.textsize(label, font)

                top, left, bottom, right = box
                print(label, (left, top), (right, bottom))

                if top - label_size[1] >= 0:
                    text_origin = np.array([left, top - label_size[1]])
                else:
                    text_origin = np.array([left, top + 1])

                # My kingdom for a good redistributable image drawing library.
                for i in range(thickness):
                    draw.rectangle([left + i, top + i, right - i, bottom - i],
                                   outline=self.colors[c])
                draw.rectangle(
                    [tuple(text_origin),
                     tuple(text_origin + label_size)],
                    fill=self.colors[c])
                draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw
            print(end - start)
            return image
        else:
            return out_boxes, out_scores, out_classes
Esempio n. 15
0
    def evaluate(self, annotation_path, iou_threshold=0.5, save_img=False):
        '''Evaluate a given dataset using a given model.
        # Arguments
            model           : The model to evaluate.
            annotation_path : each row in imgset_lable file like this:
                              imgpath x_min1,y_min1,x_max1,y_max1,0 x_min2,y_min2,x_max2,y_max2,2......
            iou_threshold   : The threshold used to consider when a detection is positive or negative, 
                              using for mAP, not the same as iou of NMS for detections.
        # Returns
            A dict mapping class names to mAP scores.
        '''
        with open(annotation_path) as f:
            annotation_lines = f.readlines()
        all_detections = [[None for i in range(len(self.class_names))]
                          for j in range(len(annotation_lines))]
        all_annotations = [[None for i in range(len(self.class_names))]
                           for j in range(len(annotation_lines))]

        start = timer()

        for i in range(len(annotation_lines)):
            line = annotation_lines[i].split()
            image = Image.open(line[0])
            boxes = np.array([
                np.array(list(map(int, boxes.split(','))))
                for boxes in line[1:]
            ])

            for label in range(len(self.class_names)):
                all_annotations[i][label] = boxes[boxes[:, -1] == label, :-1]

            if self.model_image_size != (None, None):
                assert self.model_image_size[
                    0] % 32 == 0, 'Multiples of 32 required'
                assert self.model_image_size[
                    1] % 32 == 0, 'Multiples of 32 required'
                boxed_image = letterbox_image(
                    image, tuple(reversed(self.model_image_size)))
            else:
                new_image_size = (image.width - (image.width % 32),
                                  image.height - (image.height % 32))
                boxed_image = letterbox_image(image, new_image_size)
            image_data = np.array(boxed_image, dtype='float32')

            image_data /= 255.
            image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

            out_boxes, out_scores, out_classes = self.sess.run(
                [self.boxes, self.scores, self.classes],
                feed_dict={
                    self.yolo_model.input: image_data,
                    self.input_image_shape: [image.size[1], image.size[0]],
                    K.learning_phase(): 0
                })
            out_boxes2 = out_boxes.copy()
            for j in range(len(out_boxes2)):
                box = out_boxes2[j]
                top, left, bottom, right = box
                out_boxes2[j][1] = max(0, np.floor(top + 0.5).astype('int32'))
                out_boxes2[j][0] = max(0, np.floor(left + 0.5).astype('int32'))
                out_boxes2[j][3] = min(image.size[1],
                                       np.floor(bottom + 0.5).astype('int32'))
                out_boxes2[j][2] = min(image.size[0],
                                       np.floor(right + 0.5).astype('int32'))

            image_detections = np.concatenate([
                out_boxes2,
                np.expand_dims(out_scores, axis=1),
                np.expand_dims(out_classes, axis=1)
            ],
                                              axis=1)
            for label in range(len(self.class_names)):
                all_detections[i][label] = image_detections[
                    image_detections[:, -1] == label, :-1]

            print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

            if save_img:
                font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                          size=np.floor(3e-2 * image.size[1] +
                                                        0.5).astype('int32'))
                thickness = (image.size[0] + image.size[1]) // 300

                for m, c in reversed(list(enumerate(out_classes))):
                    predicted_class = self.class_names[c]
                    box = out_boxes[m]
                    score = out_scores[m]

                    label = '{} {:.2f}'.format(predicted_class, score)
                    draw = ImageDraw.Draw(image)
                    label_size = draw.textsize(label, font)

                    top, left, bottom, right = box
                    top = max(0, np.floor(top + 0.5).astype('int32'))
                    left = max(0, np.floor(left + 0.5).astype('int32'))
                    bottom = min(image.size[1],
                                 np.floor(bottom + 0.5).astype('int32'))
                    right = min(image.size[0],
                                np.floor(right + 0.5).astype('int32'))
                    print(label, (left, top), (right, bottom))

                    if top - label_size[1] >= 0:
                        text_origin = np.array([left, top - label_size[1]])
                    else:
                        text_origin = np.array([left, top + 1])

                    # My kingdom for a good redistributable image drawing library.
                    for n in range(thickness):
                        draw.rectangle(
                            [left + n, top + n, right - n, bottom - n],
                            outline=self.colors[c])
                    draw.rectangle(
                        [tuple(text_origin),
                         tuple(text_origin + label_size)],
                        fill=self.colors[c])
                    draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                    del draw

                image.save('results/val/val_%d.jpg' % (i))

        end = timer()
        print(end - start)
        return self._get_map(all_detections,
                             all_annotations,
                             iou_threshold=iou_threshold)
    def detect_image(self, image, num):
        start = timer()
        humanplace = []
        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        #print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        #print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300
        tempframe = str(num) + '\n'

        hunamnum = 0
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            dian_y = int((top + bottom) / 2)
            dian_x = int((left + right) / 2)
            temppoint = [dian_x, dian_y]

            #print(label)
            if 'person' in label:
                temp = label + ' (' + str(left) + ',' + str(
                    top) + ')  (' + str(right) + ',' + str(bottom) + ')' + '\n'

                humanplace.append([left, top, right, bottom])
                hunamnum = hunamnum + 1
                tracknum.append(temppoint)
            #print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            if 'person' in label:
                # My kingdom for a good redistributable image drawing library.
                for i in range(thickness):
                    draw.rectangle([left + i, top + i, right - i, bottom - i],
                                   outline=self.colors[c])
                draw.rectangle(
                    [tuple(text_origin),
                     tuple(text_origin + label_size)],
                    fill=self.colors[c])
                draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                del draw
        pointdraw = ImageDraw.Draw(image)
        for i in tracknum:
            print(len(tracknum))
            pointdraw.ellipse((i[0] - 5, i[1] - 5, i[0] + 5, i[1] + 5),
                              (255, 0, 0))
        del pointdraw
        end = timer()
        #print(end - start)
        return image, humanplace
Esempio n. 17
0
    def detect_image(self, image):
        """
        利用yolov3网络对图片进行识别
        注意 对于cv2来说,h,w,channel = shape[0],shape[1],shape[2]
        args:
            image: ndarray
        returns:
            image: ndarray
            out_boxes: 
            out_scores:  
            out_classes: 

        """
        start = timer()
        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            # 这里也要改过来
            # new_image_size = (image.width - (image.width % 32),
            #                   image.height - (image.height % 32))
            new_image_size = (image.height - (image.height % 32),
                              image.width - (image.width % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                # K.learning_phase(): 0
            })
        # return out_boxes, out_scores, out_classes
        font = ImageFont.truetype(
            font=os.path.join(gParam.Font_Path, 'ARLRDBD.TTF'),
            size=np.floor(2e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 600
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        # print('Time:', end-start)
        return image, out_boxes, out_scores, out_classes
Esempio n. 18
0
    def detect_image(self, image):
        """if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))"""

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                #K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(
            font='/usr/share/fonts/truetype/freefont/FreeMono.ttf',
            size=np.floor(8e-2 * image.size[1] - 20).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300
        left, top, right, bottom, c = -1, -1, -1, -1, -1
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom), " c: ", c, " i: ", i)

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        return c, left, top, right, bottom, image
Esempio n. 19
0
    def detect_image(self, image):
        start = timer()

        start1 = timer()
        if self._model_image_size != (None, None):
            assert self._model_image_size[0] % 32 == 0, 'Multiples of 32 required'
            assert self._model_image_size[1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self._model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.
        print("preprocess: %fs" % (timer() - start1))

        start1 = timer()
        yolo_outputs = self._sess.run(self._output_tensor_list,
                                      feed_dict={
                                          self._input_tensor: image_data
                                      })
        print("model inference: %fs" % (timer() - start1))

        start1 = timer()
        out_boxes, out_scores, out_classes = yolo_eval(yolo_outputs, self._anchors,
                                                       len(self._class_names), [image.size[1], image.size[0]],
                                                       score_threshold=self._score_thresh,
                                                       iou_threshold=self._nms_iou_thresh)
        print("cpu post process: %fs" % (timer() - start1))

        print("total time: %fs" % (timer() - start))

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 400

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self._class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for j in range(thickness):
                draw.rectangle(
                    [left + j, top + j, right - j, bottom - j],
                    outline=self._colors[c])
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self._colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        print("")

        return image
Esempio n. 20
0
    def detect_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        history = open('history_object_detection.txt', 'a')
        db = 'test.db'

        outputBuffer = BytesIO()
        image.save(outputBuffer, format='JPEG')
        imageBase64Data = outputBuffer.getvalue()
        data = base64.b64encode(imageBase64Data)
        outputBuffer.close
        sql.add_record(db, data)

        #sql.extr_record(db, './output', 1)

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # Закоментировать что бы убрать отрисовку прямоугольников
            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

            class_object = label.split(' ')[0]

            def save_object():
                area = (left, top, right, bottom)
                cropped_image = image.crop(area)
                cropped_image.save(self.object_path + class_object +
                                   '/frame_' + str(self.counter_image) + '_' +
                                   label + '.jpeg')

                outputBuffer = BytesIO()
                cropped_image.save(outputBuffer, format='JPEG')
                imageBase64Data = outputBuffer.getvalue()
                data = base64.b64encode(imageBase64Data)
                outputBuffer.close
                edit_score = '{:.2f}'.format(score)

                face_id = ''
                face_accuracy = 0

                # Face ID request
                if label.count('person'):
                    url = 'https://api.evision.tech/predict/2.0'  # Set destination URL here
                    post_fields = {
                        "key": "",
                        "minAccuracy": 30,
                        "type": "check",
                        "headers": {
                            'Content-Type': "application/json"
                        },
                        "image": "data:image/jpeg;base64," + str(data)[2:]
                    }

                    request = Request(url, urlencode(post_fields).encode())
                    resultRequest = json.loads(
                        urlopen(request).read().decode())
                    print(resultRequest)

                    sql.add_record_class(db, predicted_class)

                    if (resultRequest and resultRequest['success']
                            and resultRequest['data']
                            and len(resultRequest['data'])
                            and resultRequest['data'][0]
                            and resultRequest['data'][0]['id']
                            and resultRequest['data'][0]['accuracy']):
                        face_id = resultRequest['data'][0]['id']
                        face_accuracy = resultRequest['data'][0]['accuracy']

                        # print('face_accuracy = ', face_accuracy)
                        sql.add_record_child(db, predicted_class, edit_score,
                                             data, face_id, face_accuracy)

                #sql.child_extr_record(db, './output', 1)

                history.write('Class: ' + class_object + ' Frame: ' +
                              str(self.counter_image) + ' Probability: ' +
                              label + '\r\n')

            if not os.path.exists(self.object_path + class_object):
                try:
                    os.makedirs(self.object_path + class_object)
                    save_object()
                except OSError as e:
                    if e.errno != errno.EEXIST:
                        raise
            else:
                save_object()

        history.write('\r\n')
        history.close()
        end = timer()
        print(end - start)
        image.save(self.frames_path + str(self.counter_image) + '.jpeg')

        self.counter_image += 1

        return image
Esempio n. 21
0
    def detect_image(
            self, image
    ):  #uncomment lines 110,126,127 if helmet detection not needed
        start = timer()
        #'''

        img_arr = np.array(image)
        img_arr1 = np.array(image)
        frame_count = 0
        inpWidth = 416  #Width of network's input image
        inpHeight = 416  #Height of network's input image
        # Create a 4D blob from a frame.
        blob = cv2.dnn.blobFromImage(img_arr,
                                     1 / 255, (inpWidth, inpHeight), [0, 0, 0],
                                     1,
                                     crop=False)

        # Sets the input to the network
        hdy.net.setInput(blob)

        # Runs the forward pass to get output of the output layers
        outs = hdy.net.forward(hdy.getOutputsNames(hdy.net))

        # Remove the bounding boxes with low confidence
        helmet_boxes, score_boxes = hdy.postprocess('', img_arr, outs, '',
                                                    True)
        #'''
        #helmet_boxes = []

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        #print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        first = 0
        for ind, hbx in enumerate(helmet_boxes):
            if first == 0:
                #print(helmet_boxes)
                first = 1
            hbx = [hbx[1], hbx[0], hbx[1] + hbx[3], hbx[0] + hbx[2]]
            a = np.array(hbx)
            a = np.reshape(a, (1, a.size))
            out_boxes = np.concatenate((out_boxes, a))
            out_classes = np.concatenate((out_classes, np.array([80])))
            out_scores = np.concatenate(
                (out_scores, np.array([score_boxes[ind]])))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300
        all_boxes = []
        for i, c in reversed(list(enumerate(out_classes))):
            if c != 80:
                predicted_class = self.class_names[c]

            else:
                predicted_class = 'Helmet'
                c = 35  #for colour purpose

            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            all_boxes.append([label, left, top, right, bottom])

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        #print(end - start)
        bike_boxes = []
        rider_boxes = []
        helmet_boxes = []
        #print(all_boxes) #label,left,top,right,bottom
        for box in all_boxes:
            label, left, top, right, bottom = box
            if 'motorbike' in label.lower():
                bike_boxes.append([left, top, right, bottom])

            elif 'person' in label.lower():
                rider_boxes.append([left, top, right, bottom])

            elif 'helmet' in label.lower():
                helmet_boxes.append([left, top, right, bottom])

        for bb in bike_boxes:
            flag = 0
            for rb in rider_boxes:
                l1 = Point(bb[0], bb[3])  #590,157
                r1 = Point(bb[2], bb[1])  #668, 270
                l2 = Point(rb[0], rb[3])  #631, 42
                r2 = Point(rb[2], rb[1])  #704, 236

                if (doOverlap(l1, r1, l2, r2)):

                    for hb in helmet_boxes:
                        l1 = Point(hb[0], hb[3])  #590,157
                        r1 = Point(hb[2], hb[1])  #668, 270
                        l2 = Point(rb[0], rb[3])  #631, 42
                        r2 = Point(rb[2], rb[1])  #704, 236

                        if (doOverlap(l1, r1, l2, r2)):
                            flag = 1
                            break

            if not flag:
                for rb in rider_boxes:
                    l1 = Point(bb[0], bb[3])  #590,157
                    r1 = Point(bb[2], bb[1])  #668, 270
                    l2 = Point(rb[0], rb[3])  #631, 42
                    r2 = Point(rb[2], rb[1])  #704, 236

                    if (doOverlap(l1, r1, l2, r2)):
                        #crop
                        global ctr1
                        ctr1 += 1
                        pair = [rb, bb]
                        I = cv2.cvtColor(img_arr1, cv2.COLOR_BGR2RGB)
                        image2 = Image.fromarray(I, mode='RGB')
                        im_crop = image2.crop(
                            (max(pair[1][0] - 30,
                                 0), max(pair[0][1] - 30,
                                         0), pair[1][2] + 30, pair[1][3] + 30))
                        # Comment out below line to disable challan images popping up during execution
                        # todo control this using argparse
                        im_crop.show()

                        #parts = img.rsplit('.',1)
                        #im_crop.save(os.path.join('cropped_images',parts[0]+str(ctr)+'.'+parts[1]))
                        im_crop.save(
                            os.path.join('cropped_images',
                                         str(ctr1) + '.jpg'))
                        sleep(1)
                        #im_crop.close()
                        for proc in psutil.process_iter():
                            if proc.name() == "display":
                                proc.kill()
                        break

        return image, all_boxes
Esempio n. 22
0
    def detect_image_single(self, file, isDrawBox):
        try:
            image = Image.open(file)
            width, height = image.size
        except Exception as e:
            print("Error: ", repr(e))
            return None
        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        #print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        # print('type of out_boxes: ', type(out_boxes))
        # print('type of out_scores: ', type(out_scores))
        # print('type of out_classes: ', type(out_classes))
        #
        # print(zip(out_boxes, out_scores, out_classes))
        # for i, (a, b, c) in enumerate(zip(out_boxes, out_scores, out_classes)):
        #     print(i, a, b, self.class_names[c])

        #series = pd.Series(out_boxes[:, 0])
        df = pd.DataFrame(out_boxes)
        #df = df.rename(columns={'0':'top', '1':'left', '2':'bottom','3':'right'})
        df.columns = ['top', 'left', 'bottom', 'right']
        #print(np.floor(df['top'] + 0.5))
        df['top'] = np.maximum(0, np.floor(df['top'] + 0.5)).astype('int32')
        df['left'] = np.maximum(0, np.floor(df['left'] + 0.5)).astype('int32')
        df['bottom'] = np.minimum(height, np.floor(df['bottom'] + 0.5)).astype('int32')
        df['right'] = np.minimum(width, np.floor(df['right'] + 0.5)).astype('int32')
        #print(np.minimum(0, np.floor(df['top'] + 0.5)))

        class_names = []
        for i in out_classes:
            class_names.append(self.class_names[i])
        df['class_name'] = class_names
        df['score'] = out_scores
        df['out_classes'] = out_classes
        df['image'] = os.path.basename(file)
        df['area_pct'] = (df['bottom'] - df['top']) * (df['right'] - df['left']) / (width * height)
        df['area_pct'] = df['area_pct'].abs()
        df['x_min'] = df['left']
        df['y_min'] = df['top']
        df['width'] = df['right'] - df['left']
        df['height'] = df['bottom'] - df['top']
        # print(df)
       # print(df['class_name'])
        if isDrawBox:
            font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                      size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
            thickness = (image.size[0] + image.size[1]) // 300

            for i, c in reversed(list(enumerate(out_classes))):
                predicted_class = self.class_names[c]
                box = out_boxes[i]
                score = out_scores[i]

                label = '{} {:.2f}'.format(predicted_class, score)
                draw = ImageDraw.Draw(image)
                label_size = draw.textsize(label, font)
                #
                top, left, bottom, right = box
                top = max(0, np.floor(top + 0.5).astype('int32'))
                left = max(0, np.floor(left + 0.5).astype('int32'))
                bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
                right = min(image.size[0], np.floor(right + 0.5).astype('int32'))

                if top - label_size[1] >= 0:
                    text_origin = np.array([left, top - label_size[1]])
                else:
                    text_origin = np.array([left, top + 1])

                # My kingdom for a good redistributable image drawing library.
                for i in range(thickness):
                    draw.rectangle(
                        [left + i, top + i, right - i, bottom - i],
                        outline=self.colors[c])


                # if df.ix[i, 'top'] - label_size[1] >= 0:
                #     text_origin = np.array([df.ix[i, 'left'], df.ix[i, 'top'] - label_size[1]])
                # else:
                #     text_origin = np.array([df.ix[i, 'left'], df.ix[i, 'top'] + 1])
                #
                # # My kingdom for a good redistributable image drawing library.
                # #print(label, (left, top), (right, bottom))
                # for i in range(thickness):
                #     draw.rectangle(
                #         [df.ix[i, 'left'] + i, df.ix[i, 'top'] + i, df.ix[i, 'right'] - i, df.ix[i, 'bottom'] - i],
                #         outline=self.colors[c])
                draw.rectangle(
                    [tuple(text_origin), tuple(text_origin + label_size)],
                    fill=self.colors[c])
                draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                del draw

        return df, image
Esempio n. 23
0
    def detect_image(self, image):
        """
        A function which returns the predictions of a trained yolo model together with
        all the information that is necessary for computing the mAP scores wrt the ground truth files.
        We also return the original image with a bounding-box drawn on it if it exists.

        :param image: an image coming from the testing-set
        :return: the predicted_class, x_min, y_min, x_max, y_max and an image with the drawn bounding-boxes
        """

        detections = []
        min_coordinates = []
        max_coordinates = []

        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='../font/FiraMono-Medium.otf',
                    size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)

            detections.append(label)
            min_coordinates.append((left, top))
            max_coordinates.append((right, bottom))

            del draw

        return detections, min_coordinates, max_coordinates, image
Esempio n. 24
0
    def detecting_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))

        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)

        image_data = np.array(boxed_image, dtype='float32')
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        prediction_path = self.prediction_path
        if os.path.exists(prediction_path) == False:
            os.mkdir(prediction_path)
            print('make folder: ', prediction_path)

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        for i, c in reversed(list(enumerate(out_classes))):

            top, left, bottom, right = out_boxes[i]
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))

            # draw.rectangle([left, top, right, bottom], outline=self.colors[1])
            # del draw

            cropbox = (left, top, right, bottom)
            cropped = image.crop(cropbox)

            predicted_class = self.class_names[c]

            imageName = '{}_{}_{}_{}_{}.jpg'.format(predicted_class, left, top,
                                                    right, bottom)

            prediction_image = os.path.join(prediction_path, imageName)
            print('Saving: ', prediction_image)
            cropped.save(prediction_image)
            print(predicted_class)
            print(out_scores[i])

        end = timer()
        print('time used: %f' % (end - start))

        return
Esempio n. 25
0
    def detect_image(self, image, dir):
        start = timer()
        name = dir.split('\\')

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300
        image_crop = image
        count = 0  # add
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)

            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            #print(label, (left, top), (right, bottom))

            image_crop = image.crop((left, top, right, bottom))
            print(left, top, right, bottom)
            #draw = ImageDraw.Draw(image_crop)
            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            image_crop.save('./' + str(count) + "_" + "(" + str(left) + "," +
                            str(top) + ")" + "(" + str(right) + "," +
                            str(bottom) + ")" + "_" + name[-1])
            count = count + 1
            # My kingdom for a good redistributable image drawing library.
            """      (draw detect box )20190306
            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw
            """

        end = timer()
        print(end - start)
        return image_crop
Esempio n. 26
0
    def detect_image(self, image, query_input, frame_id):

        start = timer()
        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                    size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        # Initialize a counter to count car per frame
        count = 0
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]
            label = '{} {:.2f}'.format(predicted_class, score)
            
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # check if predicted object is a car
            if(predicted_class == "car"): 
                # get the region of interest indices
                boundry_box = (left, top, right, bottom)
                # Crop image to region of interest
                cropped_image = self.crop_image(image,boundry_box)
                # Query 1
                if(query_input == '1'):
                    # Start timer
                    prev_time = timer()
                    # Increment count
                    count += 1
                    # Add Count to the label
                    label = '{}, count = {}'.format(predicted_class, count)
                    label_size = draw.textsize(label, font)
                    # Draw bounding box
                    for i in range(thickness):
                        draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c])
                    draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c])
                    draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                    del draw
                    # Stop timer
                    curr_time = timer()
                    # Record execution time
                    time = curr_time - prev_time
                    # Add time and count details of the frame to the dictionary
                    self.result_dictionary[frame_id] = {'count':count, 'time': time}
                elif(query_input == '2'):
                    # Start timer
                    prev_time = timer()
                    # Increment count
                    count += 1
                    # get car type
                    carType = self.carTypeObj.predict_image(cropped_image)
                    # Add car type and count to the label
                    label = '{}, count = {}, {}'.format(predicted_class, count, carType)
                    label_size = draw.textsize(label, font)
                    # Draw bounding box
                    for i in range(thickness):
                        draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c])
                    draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c])
                    draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                    del draw
                    # Stop Timer
                    curr_time = timer()
                    # Record execution time
                    time = curr_time - prev_time
                    # Add car type, count and time details of the frame to the dictionary
                    self.result_dictionary[frame_id] = {'count': count, carType: 1, 'time': time}
                elif(query_input == '3'):
                    # Start timer
                    prev_time = timer()
                    # Increment count
                    count += 1
                    # get car type
                    carType = self.carTypeObj.predict_image(cropped_image)
                    # get car color
                    carColor = self.colorObj.detect_color(cropped_image)
                    # Add color, type and count of the car to the label
                    label = '{}, count = {}, {}, {}'.format(predicted_class, count, carType, carColor)
                    label_size = draw.textsize(label, font)
                    # Draw bounding box
                    for i in range(thickness):
                        draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c])
                    draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c])
                    draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                    del draw
                    # Stop Timer
                    curr_time = timer()
                    # Record execution time
                    time = curr_time - prev_time
                    # Add color, type, count and time details of the frame to the dictionary
                    self.result_dictionary[frame_id] = {'count': count, carType: 1, carColor: 1, 'time': time}
                else:
                    print("Query Input Received : ", query_input)
        count = 0
        end = timer()
        print(end - start)
        return image
Esempio n. 27
0
    def detect_with_onnx(self, image):
        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')
        image_data /= 255.
        image_data = np.transpose(image_data, [2, 0, 1])

        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.
        feed_f = dict(
            zip(['input_1', 'image_shape'],
                (image_data,
                 np.array([image.size[1], image.size[0]],
                          dtype='float32').reshape(1, 2))))
        all_boxes, all_scores, indices = self.session.run(None,
                                                          input_feed=feed_f)

        out_boxes, out_scores, out_classes = [], [], []
        for idx_ in indices:
            out_classes.append(idx_[1])
            out_scores.append(all_scores[tuple(idx_)])
            idx_1 = (idx_[0], idx_[2])
            out_boxes.append(all_boxes[idx_1])

        font = ImageFont.truetype(
            font=self._get_data_path('font/FiraMono-Medium.otf'),
            size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        return image
Esempio n. 28
0
    def detect_image(self, image):
        start = timer()  # 起始时间

        if self.model_image_size != (
                None, None):  # 416x416, 416=32*13,必须为32的倍数,最小尺度是除以32
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image,
                                          tuple(reversed(
                                              self.model_image_size)))  # 填充图像
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')
        print('detector size {}'.format(image_data.shape))
        image_data /= 255.  # 转换0~1
        image_data = np.expand_dims(image_data, 0)  # 添加批次维度,将图片增加1维

        # 参数盒子、得分、类别;输入图像0~1,4维;原始图像的尺寸
        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'image'))  # 检测出的框

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))  # 字体
        thickness = (image.size[0] + image.size[1]) // 512  # 厚度
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]  # 类别
            box = out_boxes[i]  # 框
            score = out_scores[i]  # 执行度

            label = '{} {:.2f}'.format(predicted_class, score)  # 标签
            draw = ImageDraw.Draw(image)  # 画图
            label_size = draw.textsize(label, font)  # 标签文字

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))  # 边框

            if top - label_size[1] >= 0:  # 标签文字
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):  # 画框
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(  # 文字背景
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)  # 文案
            del draw

        end = timer()
        print(end - start)  # 检测执行时间
        return image
Esempio n. 29
0
    def button_open_image_click(self):
        imgName, imgType = QFileDialog.getOpenFileName(
            self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
        image = Image.open(imgName)
        print("单幅图像", image.mode)
        # r_image = detect_image(image)
        # r_image.show()
        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)

        image_data = np.array(boxed_image, dtype='float32')

        # print(" image_data.shape:",image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            # print(label, (left, top), (right, bottom))
            center_y = (top + bottom) / 2
            center_x = (left + right) / 2
            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
                draw.point((center_x, center_y), fill=(255, 0, 0))
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw
        self.result = np.asarray(image)
        self.result = cv2.cvtColor(self.result, cv2.COLOR_RGB2BGR)
        # cv2.imwrite('1.jpg',self.result)
        self.result = cv2.cvtColor(self.result, cv2.COLOR_BGR2BGRA)
        self.result = cv2.resize(self.result, (640, 480),
                                 interpolation=cv2.INTER_AREA)
        # self.result = cv2.cvtColor(self.result, cv2.COLOR_BGRA2RGB)
        # result = frame
        self.QtImg = QtGui.QImage(self.result.data, self.result.shape[1],
                                  self.result.shape[0],
                                  QtGui.QImage.Format_RGB32)
        # 显示图片到label中;
        self.showimage.setPixmap(QtGui.QPixmap.fromImage(self.QtImg))
Esempio n. 30
0
    def detect_image(self, image):
        if self.is_fixed_size:
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        #print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        return_boxs = []
        return_class_name = []
        person_counter = 0
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            #print(self.class_names[c])
            '''
            if predicted_class != 'person' and predicted_class != 'car':
               print(predicted_class)
               continue
            '''
            if predicted_class != args["class"]:
                #print(predicted_class)
                continue

            person_counter += 1
            #if  predicted_class != 'car':
            #continue
            #label = predicted_class
            box = out_boxes[i]
            #score = out_scores[i]
            x = int(box[1])
            y = int(box[0])
            w = int(box[3] - box[1])
            h = int(box[2] - box[0])
            if x < 0:
                w = w + x
                x = 0
            if y < 0:
                h = h + y
                y = 0
            return_boxs.append([x, y, w, h])
            #print(return_boxs)
            return_class_name.append([predicted_class])
        #cv2.putText(image, str(self.class_names[c]),(int(box[0]), int(box[1] -50)),0, 5e-3 * 150, (0,255,0),2)
        #print("Found person: ",person_counter)
        return return_boxs, return_class_name
Esempio n. 31
0
    def detect_from_img(self, image, frame):
        start = timer()
        #origin_img = image
        #origin_img = cv2.cvtColor(np.asarray(origin_img), cv2.COLOR_RGB2BGR)
        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        #out_boxes = [i for i in out_boxes if i in ['bus','car','person']]
        #out_scores = [i for i in out_scores if i in ['bus','car','person']]
        out_classes = [i for i in out_classes if self.class_names[i] in ['bus','car','person']]
        result_info = 'Found {} boxes:\n'.format(len(out_boxes))
        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                    size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 400

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            if predicted_class == 'car' or predicted_class == 'bus':
                box = [top, bottom, left, right]
                print(type(frame))
                carnumber = self.detect_carnumber(frame, box, [20,20])
                if carnumber is None:
                    carnumber = 'no'
                else:
                    carnumber = ''.join(carnumber)
                print('carnumber',carnumber)
                label = '{} {}'.format(predicted_class, carnumber)  #label = '{} {:.2f}'
            else:
                label = '{} {:.1f}'.format(predicted_class, score)  #label = '{} {:.2f}'
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)
            result_info += label
            result_info += '\n'
            print(label)#(left, top), (right, bottom)

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw


        end = timer()
        print(end - start)
        return image, result_info
Esempio n. 32
0
    def detect_image(self, image):
        start = timer()

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        #print(image_data.shape)
        image_data /= 255.
        #print('m1')
        image_data = np.expand_dims(image_data, 0)
        #print('m2')
        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })
        #print('m3');

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(
                0,
                np.floor(top + 0.5).astype('int32') -
                0.07 * np.floor(top + 0.5).astype('int32'))
            left = max(
                0,
                np.floor(left + 0.5).astype('int32') -
                0.07 * np.floor(left + 0.5).astype('int32'))
            bottom = min(
                image.size[1],
                np.floor(bottom + 0.5).astype('int32') +
                0.07 * np.floor(bottom + 0.5).astype('int32'))
            right = min(
                image.size[0],
                np.floor(right + 0.5).astype('int32') +
                0.07 * np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            #if (right-left / bottom-top > 2):

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        print(end - start)
        return image