-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaPipe.py
146 lines (122 loc) · 6.12 KB
/
MediaPipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from utils import *
import cv2
import mediapipe as mp
import os
import numpy as np
from pprint import pprint
import math
import pickle
from google.protobuf import text_format
from google.protobuf.json_format import MessageToDict
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
makeFolder("augmentedImgsMediaPipe")
for augment in os.listdir("augmentedImgs"):
makeFolder("augmentedImgsMediaPipe/" + augment + "-mediaPipe")
allImagesPathGrouped = []
for imgNumber in range(100):
eachImgAugments = []
for augment in sorted(os.listdir("augmentedImgs")):
eachImgAugments.append("augmentedImgs/" + augment + "/" + sorted(os.listdir("augmentedImgs/" + augment))[imgNumber])
allImagesPathGrouped.append(eachImgAugments)
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
color = (255, 0, 0)
thickness = 3
resultsArray = []
makeFolder("allEditsStitched")
l = 0
for imgGroup in allImagesPathGrouped:
count = 0
cleanImgPath = os.path.split(imgGroup[0])[1][0:3] + ".jpg"
imgEditsAll = cv2.flip(cv2.imread("random_images/" + cleanImgPath), 1)
pointCollection = []
print("-"*40)
print("Running " + imgGroup[0])
tipFingerNumbers = {}
with mp_hands.Hands(
static_image_mode=True,
max_num_hands=2,
min_detection_confidence=0.5) as hands:
for idx, file in enumerate(imgGroup):
augmentType = os.path.split(os.path.dirname(file))[1]
resultingPath = "augmentedImgsMediaPipe/" + augmentType + "-mediaPipe/" + os.path.split(file)[1]
detailsArray = []
image = cv2.flip(cv2.imread(file), 1)
results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Print handedness and draw hand landmarks on the image.
# print('Handedness:', results.multi_handedness)
if not results.multi_hand_landmarks:
print(f"\t{count} - Failed in {augmentType}")
cv2.imwrite(resultingPath, cv2.imread(file))
count+=1
continue
image_height, image_width, _ = image.shape
annotated_image = image.copy()
for hand_landmarks in results.multi_hand_landmarks:
if augmentType == "rotatedImg-45" or augmentType == "rotatedImg-90" or augmentType == "flippedImg":
dictLandmark = MessageToDict(hand_landmarks)
print("\tChanged------------" + augmentType + "-------")
countLandMark = 0
for point in dictLandmark['landmark']:
xChanged, yChanged = changePoint(augmentType, point['x'], point['y'], normalized=True)
hand_landmarks.landmark[countLandMark].x = xChanged/image_width
hand_landmarks.landmark[countLandMark].y = yChanged/image_height
countLandMark+=1
pointCollection.append([augmentType, MessageToDict(hand_landmarks)])
#print(
# f'Index finger tip coordinates: (',
# f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width}, '
# f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_height})'
#)
mp_drawing.draw_landmarks(
annotated_image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
mp_drawing.draw_landmarks(
imgEditsAll,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
count+=1
print(f"\t{count} - Got it for {augmentType}")
#for hand_landmarks in results.multi_hand_landmarks:
xTxt = int(hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width)
yTxt = int(hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_height)
xTipP, yTipP = changePoint(augmentType, xTxt, yTxt)
tipFingerNumbers.update({augmentType : (xTipP, yTipP)})
newxTxt = annotated_image.shape[1] - xTxt - 1
newyTxt = yTxt - 10
annotated_image = cv2.flip(annotated_image, 1)
cv2.putText(annotated_image, "(" + str(newxTxt) + "," + str(yTxt) + ")", (newxTxt, newyTxt),
font, fontScale, color, thickness, cv2.LINE_AA)
cv2.putText(annotated_image, str(file[-6:]), (0,40), font, fontScale, color, thickness, cv2.LINE_AA)
cv2.putText(imgEditsAll, augmentType, (xTipP, yTipP), font, fontScale, color, thickness, cv2.LINE_AA)
cv2.imwrite(resultingPath, annotated_image)
# For display to user
# windowName = "Image" + str(file)
# percentSmall = 0.55
# size = ((int) (annotated_image.shape[1]*percentSmall), (int) (annotated_image.shape[0]*percentSmall))
# annotated_image = cv2.resize(annotated_image, size)
#print("")
#print("\t" + file[-6:])
#print("\tSize--- " + str(annotated_image.shape[0]) + ", " + str(annotated_image.shape[1]))
#print("\tFinger--- "+ str(newxTxt) + ", " + str(yTxt))
#print("-"*20)
#cv2.imshow(windowName, annotated_image)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
cv2.imwrite("allEditsStitched/" + os.path.split(imgGroup[0])[1][0:3] + "-ALL.jpg", imgEditsAll)
print("The file changed!")
#pprint(tipFingerNumbers)
print(f"{count} Different drawings on the image")
resultsArray.append(pointCollection)
if l == 5:
break
l += 1
with open('data.pickle', 'wb') as f:
pickle.dump(resultsArray, f)