Example #1
0
def transform_img(img_name, bbox):
    if img_name[:4] == 'http':
        type_ = 'url'
    else:
        type_ = 'path'
    img = cv_load_image(img_name, type_)  #.astype(np.float32)

    # print bbox
    # print len(bbox)
    # raise
    bbox = np.array(bbox).reshape((1, -1))
    bbox = scale_bbox(bbox, 1.1).astype(int)[0]
    H, W, _ = img.shape
    bbox[2] = min(bbox[2], W)
    bbox[3] = min(bbox[3], H)
    img = img[bbox[1]:bbox[3], bbox[0]:bbox[2]]

    img = cv2.resize(img, (224, 224))

    #cv2.imshow('img',img)
    #cv2.waitKey(0)
    #hsv = img
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    hsv = hsv.transpose((2, 0, 1))
    #print hsv.shape
    #mean = np.array([104.0, 117.0, 123.0], dtype=np.float32).reshape((1, 1, -1))
    #img -= mean
    #img = img.transpose((2, 0, 1))
    return hsv
Example #2
0
def transform_img(img_name, bbox):
    if img_name[:4] == 'http':
        type_ = 'url'
    else:
        type_ = 'path'
    img = cv_load_image(img_name, type_).astype(np.float32)
    #img = cv_load_image(img_name, type_)
    # print bbox
    # print len(bbox)
    # raise
    bbox = np.array(bbox).reshape((1, -1))
    bbox = scale_bbox(bbox, 1.1).astype(int)[0]
    H, W, _ = img.shape
    bbox[2] = min(bbox[2], W)
    bbox[3] = min(bbox[3], H)
    img = img[bbox[1]:bbox[3], bbox[0]:bbox[2]]
    img = cv2.resize(img, (224, 224))
    #plt.subplot(211)
    #plt.imshow(img[:,:,(2,1,0)])
    img = 1.2*img
    img[img>255] = 255
    #plt.subplot(212)
    #plt.imshow(img[:,:,(2,1,0)])
    #plt.show()
    mean = np.array([104.0, 117.0, 123.0], dtype=np.float32).reshape((1, 1, -1))
    img -= mean
    img = img.transpose((2, 0, 1))
    return img
Example #3
0
def prep_im_for_blob_with_bbox(im, bbox, BBOX_SCALE):
    im_old = im.astype(np.float32, copy=False)
    bbox_old = bbox[:]
    im = im.astype(np.float32, copy=False)
    if config.BBOX_SCALE_TYPE == 'SCALE':
        bbox = np.array(bbox).reshape((1, -1))
        bbox = scale_bbox(bbox, config.BBOX_SCALE).astype(int)[0]
    elif config.BBOX_SCALE_TYPE == 'ABSOLUTE':
        bbox[0] -= int(BBOX_SCALE)
        bbox[1] -= int(BBOX_SCALE)
        bbox[2] += int(BBOX_SCALE)
        bbox[3] += int(BBOX_SCALE)
    else:
        raise

    H, W, _ = im.shape
    bbox[0] = max(bbox[0], 0)
    bbox[1] = max(bbox[1], 0)
    bbox[2] = min(bbox[2], W)
    bbox[3] = min(bbox[3], H)

    im = im[bbox[1]:bbox[3], bbox[0]:bbox[2]]
    target_size = config.TARGET_SIZE
    pixel_means = np.array([[config.PIXEL_MEANS]])
    if im.shape[0] * im.shape[1] == 0:
        print im_old.shape, im.shape
        print bbox_old, bbox
    im = cv2.resize(im, (target_size, target_size),
                    interpolation=cv2.INTER_LINEAR)
    im -= pixel_means
    return im
Example #4
0
def transform_img(img_name, bbox):
    if img_name[:4] == 'http':
        type_ = 'url'
    else:
        type_ = 'path'
    img = cv_load_image(img_name, type_)  #.astype(np.float32)
    #plt.subplot(236), plt.imshow(img[:,:,(2,1,0)])
    # print bbox
    # print len(bbox)
    # raise
    bbox = np.array(bbox).reshape((1, -1))
    bbox = scale_bbox(bbox, 1.1).astype(int)[0]
    H, W, _ = img.shape
    bbox[2] = min(bbox[2], W)
    bbox[3] = min(bbox[3], H)
    img = img[bbox[1]:bbox[3], bbox[0]:bbox[2]]
    #plt.subplot(234), plt.imshow(img)
    #plt.subplot(235), plt.imshow(img[:,:,(2,1,0)])
    #img = cv2.resize(img, (224, 224))

    #cv2.imshow('img',img)
    #cv2.waitKey(0)
    #hsv = img
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    #H, S, V = cv2.split(HSV)
    #print hsv
    #print hsv.shape


    hist = [cv2.calcHist([hsv], [0], None, [50], [0, 180]), \
    cv2.calcHist([hsv], [1], None, [50], [0, 256]), \
    cv2.calcHist([hsv], [2], None, [50], [0, 256])]
    hist = np.array(hist)
    x = np.arange(50) + 0.5
    #plt.subplot(231), plt.bar(x, hist[0])
    #plt.subplot(232), plt.bar(x, hist[1])
    #plt.subplot(233), plt.bar(x, hist[2])
    #plt.bar(x, hist[0])
    #plt.savefig('img/hist.jpg')
    #hsv = hsv.transpose((2, 0, 1))
    #mean = np.array([104.0, 117.0, 123.0], dtype=np.float32).reshape((1, 1, -1))
    #img -= mean
    #img = img.transpose((2, 0, 1))
    return img, hsv, hist