Pythonを使用して骨格情報の抽出を行う画像処理をしています。
現在使用しているプログラムでは、1枚ずつしか処理を行う事が出来ず、1000枚ほどの画像処理をループ処理で行うプログラムを作成したいです。
FromImgName = r'抽出前の元画像ファイル名'
ToImgName = r'抽出後の画像格納ファイル名'
files = os.listdir(FromImgName)
上記をclass HandDetector文の前に記載し、for文にてループ処理を行うようにしました。
しかし、画像すら読み込むことが出来ず、処理が行えませんでした。
また、メモ帳からバッチファイルを作成してコマンドプロンプトにかけたのですが、処理すら行えませんでした。
ファイル名を指定し、そのファイル内の画像をまとめて骨格情報抽出を行いたいです。
抽出前の画像の名前をそのまま抽出後の画像の名前にしたいです。
現在は、コマンドプロンプトにて画像を指定して処理を行っています。
python hand_detector.py handnet models/handnet.npz --img data/hand.png
該当のソースコード
Python
1ソースコード 2```import cv2 3import argparse 4import numpy as np 5from scipy.ndimage.filters import gaussian_filter 6import chainer 7from chainer import cuda, serializers, functions as F 8from entity import params 9from models.HandNet import HandNet 10 11chainer.using_config('enable_backprop', False) 12 13class HandDetector(object): 14 def __init__(self, arch=None, weights_file=None, model=None, device=-1): 15 print('Loading HandNet...') 16 self.model = params['archs'][arch]() 17 serializers.load_npz(weights_file, self.model) 18 19 self.device = device 20 if self.device >= 0: 21 cuda.get_device_from_id(device).use() 22 self.model.to_gpu() 23 24 # create gaussian filter 25 ksize = params['ksize'] 26 kernel = cuda.to_gpu(self.create_gaussian_kernel(sigma=params['gaussian_sigma'], ksize=ksize)) 27 self.gaussian_kernel = kernel 28 29 def __call__(self, hand_img, fast_mode=False, hand_type="right"): 30 if hand_type == "left": 31 hand_img = cv2.flip(hand_img, 1) 32 33 hand_img_h, hand_img_w, _ = hand_img.shape 34 35 resized_image = cv2.resize(hand_img, (params["hand_inference_img_size"], params["hand_inference_img_size"])) 36 x_data = np.array(resized_image[np.newaxis], dtype=np.float32).transpose(0, 3, 1, 2) / 256 - 0.5 37 38 if self.device >= 0: 39 x_data = cuda.to_gpu(x_data) 40 41 hs = self.model(x_data) 42 heatmaps = F.resize_images(hs[-1], (hand_img_h, hand_img_w)).data[0] 43 44 if self.device >= 0: 45 heatmaps = heatmaps.get() 46 47 if hand_type == "left": 48 heatmaps = cv2.flip(heatmaps.transpose(1, 2, 0), 1).transpose(2, 0, 1) 49 50 keypoints = self.compute_peaks_from_heatmaps(heatmaps) 51 52 return keypoints 53 54 # compute gaussian filter 55 def create_gaussian_kernel(self, sigma=1, ksize=5): 56 center = int(ksize / 2) 57 kernel = np.zeros((1, 1, ksize, ksize), dtype=np.float32) 58 for y in range(ksize): 59 distance_y = abs(y-center) 60 for x in range(ksize): 61 distance_x = abs(x-center) 62 kernel[0][0][y][x] = 1/(sigma**2 * 2 * np.pi) * np.exp(-(distance_x**2 + distance_y**2)/(2 * sigma**2)) 63 return kernel 64 65 def compute_peaks_from_heatmaps(self, heatmaps): 66 keypoints = [] 67 xp = cuda.get_array_module(heatmaps) 68 69 if xp == np: 70 for i in range(heatmaps.shape[0] - 1): 71 heatmap = gaussian_filter(heatmaps[i], sigma=params['gaussian_sigma']) 72 max_value = heatmap.max() 73 if max_value > params['hand_heatmap_peak_thresh']: 74 coords = np.array(np.where(heatmap==max_value)).flatten().tolist() 75 keypoints.append([coords[1], coords[0], max_value]) # x, y, conf 76 else: 77 keypoints.append(None) 78 else: 79 heatmaps = F.convolution_2d(heatmaps[:, None], self.gaussian_kernel, stride=1, pad=int(params['ksize']/2)).data.squeeze().get() 80 for heatmap in heatmaps[:-1]: 81 max_value = heatmap.max() 82 if max_value > params['hand_heatmap_peak_thresh']: 83 coords = np.array(np.where(heatmap==max_value)).flatten().tolist() 84 keypoints.append([coords[1], coords[0], max_value]) # x, y, conf 85 else: 86 keypoints.append(None) 87 88 return keypoints 89 90def draw_hand_keypoints(orig_img, hand_keypoints, left_top): 91 img = orig_img.copy() 92 left, top = left_top 93 94 finger_colors = [ 95 (0, 0, 255), 96 (0, 255, 255), 97 (0, 255, 0), 98 (255, 0, 0), 99 (255, 0, 255), 100 ] 101 102 for i, finger_indices in enumerate(params["fingers_indices"]): 103 for finger_line_index in finger_indices: 104 keypoint_from = hand_keypoints[finger_line_index[0]] 105 keypoint_to = hand_keypoints[finger_line_index[1]] 106 107 if keypoint_from: 108 keypoint_from_x, keypoint_from_y, _ = keypoint_from 109 cv2.circle(img, (keypoint_from_x + left, keypoint_from_y + top), 3, finger_colors[i], -1) 110 111 if keypoint_to: 112 keypoint_to_x, keypoint_to_y, _ = keypoint_to 113 cv2.circle(img, (keypoint_to_x + left, keypoint_to_y + top), 3, finger_colors[i], -1) 114 115 if keypoint_from and keypoint_to: 116 cv2.line(img, (keypoint_from_x + left, keypoint_from_y + top), (keypoint_to_x + left, keypoint_to_y + top), finger_colors[i], 1) 117 118 return img 119 120if __name__ == '__main__': 121 parser = argparse.ArgumentParser(description='Face detector') 122 parser.add_argument('arch', choices=params['archs'].keys(), default='facenet', help='Model architecture') 123 parser.add_argument('weights', help='weights file path') 124 parser.add_argument('--img', help='image file path') 125 parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)') 126 args = parser.parse_args() 127 128 # load model 129 hand_detector = HandDetector(args.arch, args.weights, device=args.gpu) 130 131 # read image 132 img = cv2.imread(args.img) 133 134 # inference 135 hand_keypoints = hand_detector(img, hand_type="right") 136 137 # draw and save image 138 b_img = np.full((600,600,3), 0, dtype = np.float32) 139 img = draw_hand_keypoints(b_img,hand_keypoints, (0, 0)) 140 print('Saving result into result.png...') 141 cv2.imwrite('result.png',img) 142 143 144### 補足情報(FW/ツールのバージョンなど) 145下記のサイトのプログラムを使用して処理を行っています。 146https://github.com/DeNA/Chainer_Realtime_Multi-Person_Pose_Estimation 147