Tensorflow ValueError:无法将大小为62842880的数组重塑为形状(4352,3610,3)

时间:2019-07-18 03:45:02

标签: python tensorflow reshape

我尝试运行用于对象检测的代码。它适用于我从互联网下载的任何普通图像。但是,我尝试获取一些缝合在一起用于映射的图片。我得到下面的错误。 reshape()函数似乎有问题。

Traceback (most recent call last):
  File "final.py", line 260, in <module>
    image_np,imwidth,imheight = load_image_into_numpy_array(image)
  File "final.py", line 213, in load_image_into_numpy_array
    return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8),im_width,im_height
ValueError: cannot reshape array of size 62842880 into shape (4352,3610,3)

由于其他论坛没有太多帮助,我没有尝试任何特别的事情。

import cv2
import numpy as np
import os
import sys,time
import tensorflow as tf
from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
import matplotlib.pyplot as plt
from PIL import Image



########################################################
###################    PARAMETERS    ###################
########################################################

cntAreaMax = 1200000
cntAreaMin = 100
dilateIte = 1
erosionIte = 5
trackbarEnable = 1


HSVmin = np.array([30,33,0])
HSVmax = np.array([68,255,75])


cntLine = 3
enableErode = 1
enableDilate = 0
color_detect = 1


obj_detect = 1
detection_classes=["tree","structure","road"]
color_BGR=[(0,255,0),(255,0,0),(0,0,255)]
starting_img_index = 3
ending_img_index = 91
detection_score_threshold = 0.5
out_img_name = "OUT-IMG-"
max_size = [4000,2000]
min_size = [600,400]
box_thickness = 3
font = cv2.FONT_HERSHEY_SIMPLEX
max_area = 500000
min_area = 100
filter_byArea = 1



######## IMPORT IMAGE HERE ########
image_name = "odm_orthophoto"
image_extension = ".tif"

PATH_TO_IMAGE = os.path.join('test_images', image_name+image_extension)
frame = cv2.imread(PATH_TO_IMAGE)
print((frame.shape[1],frame.shape[0]))

if frame.shape[1] >=1920 or frame.shape[0] >=1080:
    if frame.shape[0]>frame.shape[1]:
        fc=frame.shape[0]/1080
        tuple = (int(frame.shape[1]//fc),int(frame.shape[0]//fc))
        frame2 = cv2.resize(frame,tuple)
        print(1)
    elif frame.shape[1]>frame.shape[0]:
        fc = frame.shape[1]/1920
        #tuple = (int(frame.shape[1]//fc),int(frame.shape[0]//fc))
        tuple=(1600,800)
        frame2 = cv2.resize(frame,tuple)
        print(2)
    else:
        fc = frame.shape[1]/1080
        tuple = (int(frame.shape[1]//fc),int(frame.shape[0]//fc))
        frame2 = cv2.resize(frame,tuple)
        print(3)
    print(tuple)
else:
    frame2=frame

if color_detect:
    if trackbarEnable:
        def nothing(x):
            pass
        cv2.namedWindow("Trackbars")
        cv2.createTrackbar("L - H", "Trackbars", 0, 179, nothing)
        cv2.createTrackbar("L - S", "Trackbars", 0, 255, nothing)
        cv2.createTrackbar("L - V", "Trackbars", 0, 255, nothing)
        cv2.createTrackbar("U - H", "Trackbars", 179, 179, nothing)
        cv2.createTrackbar("U - S", "Trackbars", 255, 255, nothing)
        cv2.createTrackbar("U - V", "Trackbars", 255, 255, nothing)
        while True:
            ##
            hsv = cv2.cvtColor(frame2, cv2.COLOR_BGR2HSV)
            l_h = cv2.getTrackbarPos("L - H", "Trackbars")
            l_s = cv2.getTrackbarPos("L - S", "Trackbars")
            l_v = cv2.getTrackbarPos("L - V", "Trackbars")
            u_h = cv2.getTrackbarPos("U - H", "Trackbars")
            u_s = cv2.getTrackbarPos("U - S", "Trackbars")
            u_v = cv2.getTrackbarPos("U - V", "Trackbars")
            lower_blue = np.array([l_h, l_s, l_v])
            upper_blue = np.array([u_h, u_s, u_v])
            mask = cv2.inRange(hsv, lower_blue, upper_blue)
            result = cv2.bitwise_and(frame2, frame2, mask=mask)
            #cv2.imshow("frame", frame)
            #cv2.imshow("mask", mask)
            cv2.imshow("result", result)
            key = cv2.waitKey(1)
            if key == 27:
                break
        print("HSV RANGE: H:{}-{}, S:{}-{}, V:{}-{}".format(l_h,u_h,l_v,u_v,l_s,u_s))
        cv2.destroyAllWindows()
        lower_blue=np.array([l_h,l_s,l_v])
        upper_blue=np.array([u_h,u_s,u_v])
    else:
        lower_blue=HSVmin
        upper_blue=HSVmax


    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    res = cv2.inRange(hsv, lower_blue, upper_blue)
    cv2.imwrite("1-Range.jpg",res)
    temp = res
    res = 255-res
    element=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
    cv2.imwrite("2-MORPHELI.jpg",element)
    if enableDilate:
        dil = cv2.dilate(res,element,iterations = dilateIte)
        cv2.imwrite("4-DILATE.jpg",dil)
    if enableErode:
        res = cv2.erode(res, element, iterations=erosionIte)
        cv2.imwrite("3-ERODE.jpg",res)

    params = cv2.SimpleBlobDetector_Params()
    contours, _ = cv2.findContours(res, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    counter = 0
    ji = 0
    for contour in contours:
        if cv2.contourArea(contour) > cntAreaMin and cv2.contourArea(contour)<=cntAreaMax:
            mask = np.zeros(frame.shape[:2], np.uint8)
            cv2.drawContours(mask, contour, -1, 255, -1)

            mean = cv2.mean(frame, mask=mask)
            #print(type(np.asarray(mean)))
            mean = cv2.cvtColor(np.uint8([[[mean[0],mean[1],mean[2]]]]), cv2.COLOR_BGR2HSV)
            #print(mean)
            cv2.drawContours(frame, contour, -1, (0, 255, 0), cntLine)
            counter+=1

    cv2.imwrite("12-MASK.jpg",mask)
    cv2.imwrite("11-CONTOUR.jpg",frame)

    params.filterByColor = True
    params.blobColor = 0
    params.minThreshold = 0
    params.maxThreshold = 100
    #params.blobColor = 0
    params.minArea = 100
    params.maxArea = 1000000
    params.filterByCircularity = False
    params.filterByConvexity = False
    params.minCircularity = 0
    params.maxCircularity = 1

    det = cv2.SimpleBlobDetector_create(params)
    keypts = det.detect(res)
    i = 0

    print("CONTOUR LENGTH: %d" %counter)
    print("BLOBS IN CONTOUR: %d" %i)
    #res = cv2.cvtColor(res, cv2.COLOR_HSV2BGR)
    #print(res[3000-1,4000-1])
    cv2.imwrite("9-OUT.jpg",res)


if obj_detect:
    print("Initializing Tensorflow")
    # This is needed since the notebook is stored in the object_detection folder.
    sys.path.append("..")
    from object_detection.utils import ops as utils_ops

    if StrictVersion(tf.__version__) < StrictVersion('1.12.0'):
        raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')

    from utils import label_map_util
    from utils import visualization_utils as vis_util
    print("Preparing Object Detection Model")

    MODEL_NAME = 'output_inference_graph_v2.pb'
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'
    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('annotations', 'label_map.pbtxt')
    # ## Load a (frozen) Tensorflow model into memory.

    print("Importing Label Map")
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    # ## Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine


    category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

    def load_image_into_numpy_array(image):
        (im_width, im_height) = image.size
        return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8),im_width,im_height

    print("Organizing Images")
    IMAGE_SIZE = (12, 8)

    def run_inference_for_single_image(image, graph):
        with graph.as_default():
            with tf.Session() as sess:
                # Get handles to input and output tensors
                ops = tf.get_default_graph().get_operations()
                all_tensor_names = {output.name for op in ops for output in op.outputs}
                tensor_dict = {}
                for key in ['num_detections', 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks']:
                    tensor_name = key + ':0'
                    if tensor_name in all_tensor_names:
                        tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(tensor_name)
                if 'detection_masks' in tensor_dict:
                    # The following processing is only for single image
                    detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
                    detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
                    # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
                    real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
                    detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
                    detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
                    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(detection_masks, detection_boxes, image.shape[1], image.shape[2])
                    detection_masks_reframed = tf.cast(tf.greater(detection_masks_reframed, 0.5), tf.uint8)
                    # Follow the convention by adding back the batch dimension
                    tensor_dict['detection_masks'] = tf.expand_dims(detection_masks_reframed, 0)

                image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')

                # Run inference
                output_dict = sess.run(tensor_dict,feed_dict={image_tensor: image})

                # all outputs are float32 numpy arrays, so convert types as appropriate
                output_dict['num_detections'] = int(output_dict['num_detections'][0])
                output_dict['detection_classes'] = output_dict['detection_classes'][0].astype(np.int64)
                output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
                output_dict['detection_scores'] = output_dict['detection_scores'][0]
                if 'detection_masks' in output_dict:
                    output_dict['detection_masks'] = output_dict['detection_masks'][0]
        return output_dict

    print("Opening Image"+image_name)
    image = Image.open(PATH_TO_IMAGE)
    # the array based representation of the image will be used later in order to prepare the
    # result image with boxes and labels on it.
    image_np,imwidth,imheight = load_image_into_numpy_array(image)

    if imwidth >= max_size[0] and imheight >= max_size[1]:
        thickness = 5
        fontsize = 0.9
    elif imwidth <= min_size[0] and imheight <= min_size[1]:
        thickness = 1
        fontsize = 0.5
    else:
        thickness = 3
        fontsize = 0.7

    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_np_expanded = np.expand_dims(image_np, axis=0)
    # Actual detection.
    print("Running Object Detection on Image"+image_name)
    timer=time.time()
    output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
    # Visualization of the results of a detection.
    #print(output_dict['detection_boxes'])
    #vis_util.visualize_boxes_and_labels_on_image_array(image_np,output_dict['detection_boxes'],output_dict['detection_classes'],output_dict['detection_scores'],category_index,instance_masks=output_dict.get('detection_masks'),use_normalized_coordinates=True,line_thickness=8)
    coords=[]
    ci=0
    for i in output_dict['detection_boxes']:
        cj=0
        array=[]
        for j in i:
            if cj%2==0:
                val=output_dict['detection_boxes'][ci][cj]*imheight
            else:
                val=output_dict['detection_boxes'][ci][cj]*imwidth
            array.append(val)
            cj+=1
        coords.append(array)
        ci+=1
    accepted_indeces=[]
    sci=0
    for sc in output_dict['detection_scores']:
        if sc >= detection_score_threshold:
            accepted_indeces.append(sci)
        sci+=1
    #vis_util.visualize_boxes_and_labels_on_image_array(image_np,output_dict['detection_boxes'],output_dict['detection_classes'],output_dict['detection_scores'],category_index,instance_masks=output_dict.get('detection_masks'),use_normalized_coordinates=True,line_thickness=8)
    print("Saving as "+out_img_name+"{}. Detection Time: {}".format(starting_img_index, time.time()-timer))
    ck=0
    boxes=[]
    num_boxes = 0
    for k in output_dict['detection_boxes']:
        if ck in accepted_indeces:
            w = -int(coords[ck][1]-coords[ck][3])
            h = -int(coords[ck][0]-coords[ck][2])
            x = int((coords[ck][1]+coords[ck][3])/2)
            y = int((coords[ck][0]+coords[ck][2])/2)
            boxes.append([x,y,w,h])
            if res[y,x]==0:
                if filter_byArea:
                    if w*h <= max_area and w*h >= min_area:
                        cv2.rectangle(image_np,(int(coords[ck][1]),int(coords[ck][0])),(int(coords[ck][3]),int(coords[ck][2])),color_BGR[output_dict['detection_classes'][ck]-1],thickness)
                        cv2.putText(image_np,detection_classes[output_dict['detection_classes'][ck]-1]+": "+str(int(output_dict['detection_scores'][ck]*100))+"%",(int(coords[ck][1]),int(coords[ck][0])), font, fontsize,(255,255,255),1,cv2.LINE_AA)
                        num_boxes+=1
                        #print(w*h)
                else:
                    cv2.rectangle(image_np,(int(coords[ck][1]),int(coords[ck][0])),(int(coords[ck][3]),int(coords[ck][2])),color_BGR[output_dict['detection_classes'][ck]-1],thickness)
                    cv2.putText(image_np,detection_classes[output_dict['detection_classes'][ck]-1]+": "+str(int(output_dict['detection_scores'][ck]*100))+"%",(int(coords[ck][1]),int(coords[ck][0])), font, fontsize,(255,255,255),1,cv2.LINE_AA)
                    num_boxes+=1
                            #print(w*h)
        ck+=1
    print("Saving as "+image_name+"-OUT"+image_extension)
    cv2.imwrite(image_name+"-OUT"+image_extension, image_np)

如前所述,我遇到以下错误:

Traceback (most recent call last):
  File "final.py", line 260, in <module>
    image_np,imwidth,imheight = load_image_into_numpy_array(image)
  File "final.py", line 213, in load_image_into_numpy_array
    return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8),im_width,im_height
ValueError: cannot reshape array of size 62842880 into shape (4352,3610,3)

0 个答案:

没有答案
相关问题