0

我正在尝试检测巨大图像中的条形码,并为此训练了 Faster RCNN。要测试的图像非常大,因此需要进一步将它们分解为窗口并通过在其上运行保存的模型来对窗口进行分类。从我的代码生成的窗口是正确的,但是当尝试将它们组合到原始图像中时,我得到一个蓝色像素化图像。

#trying to combine windows with detections
import cv2
import matplotlib.pyplot as plt
import numpy as np

image = cv2.imread('37864-1598584082774.jpg') # your image path
tmp = image # for drawing a rectangle
stepSize = 500
(w_width, w_height) = (1000, 1000) # window size
for y in range(0, image.shape[0], stepSize):
      for x in range(0, image.shape[1], stepSize):
            window = image[y:y+1000,x:x+1000]
            #cv2_imshow(window)
            # classify content of the window with your classifier and  
            # determine if the window includes an object (cell) or not
            print('Running inference for window ',end='')
            image_np=np.array(window)
            input_tensor=tf.convert_to_tensor(image_np)
            input_tensor=input_tensor[tf.newaxis, ...]
            detections=detect_fn(input_tensor)
            num_detections=int(detections.pop('num_detections'))
            detections={key:value[0,:num_detections].numpy()
                          for key,value in detections.items()}
            detections['num_detections']=num_detections
            detections['detection_classes']=detections['detection_classes'].astype(np.int64)
            image_np_with_detections=image_np.copy()

            viz_utils.visualize_boxes_and_labels_on_image_array(
                      image_np_with_detections,
                      detections['detection_boxes'],
                      detections['detection_classes'],
                      detections['detection_scores'],
                      category_index,
                      use_normalized_coordinates=True,
                      max_boxes_to_draw=100,     
                      min_score_thresh=.5,      
                      agnostic_mode=False)
                           
            
            %matplotlib inline
            plt.imshow(image_np_with_detections.astype('uint8'))
            plt.show()
print('Done')
plt.show()
4

0 回答 0