extract a crop from image and remark groundtruth(if left then flip)

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 10 19:16:02 2018


@author: xiaobing.wang
"""
from skimage import transform,data
from PIL import Image
import matplotlib.pyplot as plt
import json


import os


L = []
hand_points = {'hand_pts':[]}
file_dir ="/data/xiaobing.wang/Li/Hand_v/Dataset_Hand/hand_labels/manual_train"
save_dir = "/data/xiaobing.wang/Li/Hand_v/Dataset_Hand/hand_labels/manual_train_hands"
for root, dirs, files in os.walk(file_dir):  
    for file in files: 
        if os.path.splitext(file)[1] == '.jpg':  
            #L.append(os.path.join(root, file))
            L.append(file)


for pic in L:
    img = Image.open('./manual_train/' + pic)
#    plt.imshow(img)
#    plt.show()
    with open('./manual_train/'+pic[:-4]+'.json') as f:
        print pic
        pos = json.load(f)
        head_length = float(pos['head_size'])
        is_left = pos['is_left']
        x = float(pos['hand_box_center'][0])
        y = float(pos['hand_box_center'][1])
        crop_x_left = x - head_length/2
        crop_y_up = y - head_length/2
        crop_x_right = x + head_length/2
        crop_y_down = y + head_length/2
        for p in pos['hand_pts']:
            can_see = p[2]
            hand_y = p[1] - crop_y_up
            hand_x = p[0] - crop_x_left
            print p[1]
            print hand_y
            print p[0]
            print hand_x
            #flip
            print y
            print x
            if is_left == 1:
                hand_y = (y - crop_y_up) - (hand_y - (y - crop_y_up))
                hand_x = (x - crop_x_left) - (hand_x - (x - crop_x_left))
            else:
                hand_y = hand_y
                hand_x = hand_x
                
            print hand_y
            print hand_x
                
            s = 320/head_length
            print s
            hand_y *= s
            hand_x *= s
            print hand_y
            print hand_x


            hand_points["hand_pts"].append([hand_x,hand_y,can_see])


    with open(save_dir+"/"+pic[:-4]+'.json','w') as file_obj:
        json.dump(hand_points,file_obj)
        hand_points["hand_pts"] = []


    #crop
    box = (crop_x_left,crop_y_up,crop_x_right,crop_y_down)
    roi = img.crop(box)
#    plt.imshow(roi)
#    plt.show()
    #resize
    dst = roi.resize((320,320))


    if is_left == 1:
        out = dst.transpose(Image.FLIP_LEFT_RIGHT)
    else:
        out = dst
    
#    plt.imshow(out)
#    plt.show()


    out.save('./manual_train_hands/'+pic[:-4]+'.jpg')
    
    
'''            
img = Image.open('./manual_train/000015774_01_l.jpg')
plt.imshow(img)
plt.show()
with open('./manual_train/000015774_01_l.json') as f:
    pos = json.load(f)
    head_length = float(pos['head_size'])
    is_left = pos['is_left']
    x = float(pos['hand_box_center'][0])
    y = float(pos['hand_box_center'][1])
    crop_x_left = x - head_length/2
    crop_y_up = y - head_length/2
    crop_x_right = x + head_length/2
    crop_y_down = y + head_length/2


#crop
box = (crop_x_left,crop_y_up,crop_x_right,crop_y_down)
roi = img.crop(box)
plt.imshow(roi)
plt.show()
#resize
dst = roi.resize((320,320))


if is_left == 1:
    out = roi.transpose(Image.FLIP_LEFT_RIGHT)
else:
    out = dst
    
plt.imshow(out)
plt.show()


out.save('./manual_train_hands/000015774_01_l.jpg')


#coordinate of hand points and save as a json: 
#warning:left_to_right flip of keypoint's coordinate value is important.
'''
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章