mediapipe/EXTRACT_DATA.py
2021-09-01 19:40:38 +05:30

102 lines
2.4 KiB
Python

import cv2
import mediapipe as mp
import pandas as pd
import os
import numpy as np
def image_processed(file_path):
# reading the static image
hand_img = cv2.imread(file_path)
# Image processing
# 1. Convert BGR to RGB
img_rgb = cv2.cvtColor(hand_img, cv2.COLOR_BGR2RGB)
# 2. Flip the img in Y-axis
img_flip = cv2.flip(img_rgb, 1)
# accessing MediaPipe solutions
mp_hands = mp.solutions.hands
# Initialize Hands
hands = mp_hands.Hands(static_image_mode=True,
max_num_hands=2, min_detection_confidence=0.7)
# Results
output = hands.process(img_flip)
hands.close()
try:
data = output.multi_hand_landmarks[0]
#print(data)
data = str(data)
data = data.strip().split('\n')
garbage = ['landmark {', ' visibility: 0.0', ' presence: 0.0', '}']
without_garbage = []
for i in data:
if i not in garbage:
without_garbage.append(i)
clean = []
for i in without_garbage:
i = i.strip()
clean.append(i[2:])
for i in range(0, len(clean)):
clean[i] = float(clean[i])
return([clean])
except:
return(np.zeros([1,63], dtype=int)[0])
def make_csv():
mypath = 'RESIZED_DATASET'
file_name = open('final_datasetBSL.csv', 'a')
for each_folder in os.listdir(mypath):
if '._' in each_folder:
pass
else:
for each_number in os.listdir(mypath + '/' + each_folder):
if '._' in each_number:
pass
else:
label = each_folder
file_loc = mypath + '/' + each_folder + '/' + each_number
data = image_processed(file_loc)
try:
for i in data:
file_name.write(str(i))
file_name.write(',')
file_name.write(label)
file_name.write('\n')
except:
file_name.write('0')
file_name.write(',')
file_name.write('None')
file_name.write('\n')
file_name.close()
print('Data Created !!!')
if __name__ == "__main__":
make_csv()