-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.py
171 lines (117 loc) · 4.46 KB
/
sim.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from qibullet import SimulationManager
import pybullet as p
from qibullet import PepperVirtual
import cv2
from tensorflow.keras import models
import numpy
from PIL import Image
import os,glob
import time
import random
import sys
def model_prediction(img,x, y, w, h) :
cropedImg = img[y+2:y+h-2, x+2:x+w-2]
cropedImg = cv2.resize(cropedImg, (300, 300), interpolation = cv2.INTER_AREA)
cropedImg = numpy.asarray(cropedImg)
cropedImg = numpy.expand_dims(cropedImg, 0)
cropedImg = cropedImg/ 255.0
y_pred=model.predict(cropedImg)
prediction_string = ""
if(y_pred[0][0] >= y_pred[0][1]):
prediction_string = "zemmour p=" + str(y_pred[0][0])
else:
prediction_string = "chalamet p=" + str(y_pred[0][1])
return prediction_string
if __name__ == "__main__":
simulation_manager = SimulationManager()
client_id = simulation_manager.launchSimulation(gui=True)
#######################################################
# create texture
#######################################################
# Définir les chemins vers les images à prédire,
images_path = glob.glob('chalamet_zemmour_new/chalamet_zemmour_new_300x300/*.jpg')
template_path = 'texture_template.jpg'
i = 0
# Chargement des images à prédire
for images in images_path :
img = Image.open(images)
template = Image.open(template_path)
template.paste(img,(220,607))
template.save("pictures_sim/texture_"+str(i)+".jpg")
i = i + 1
#######################################################
# create pictures
#######################################################
random_id1 = random.randint(0, 2)
random_id2 = random.randint(3, 5)
random_x1 = random.randint(5, 9)
random_x2 = random.randint(5, 9)
random_y1 = random.randint(1, 4)
random_y2 = random.randint(1, 4)
texture_1 = p.loadTexture("./pictures_sim/texture_"+str(random_id1)+".jpg")
texture_2 = p.loadTexture("./pictures_sim/texture_"+str(random_id2)+".jpg")
p.connect(p.DIRECT)
picture_visual1 = p.createVisualShape(p.GEOM_BOX, halfExtents=[1,0.01,1])
picture_visual2 = p.createVisualShape(p.GEOM_BOX, halfExtents=[1,0.01,1])
picture1 = p.createMultiBody(baseVisualShapeIndex=picture_visual1, basePosition = [random_x1, random_y1, 1], baseOrientation = [1,1,0,0])
picture2 = p.createMultiBody(baseVisualShapeIndex=picture_visual2, basePosition = [random_x2, -random_y2, 1], baseOrientation = [1,1,0,0])
p.changeVisualShape(picture_visual1, -1, textureUniqueId=texture_1)
p.changeVisualShape(picture_visual2, -1, textureUniqueId=texture_2)
#######################################################
# create robot
#######################################################
pepper = simulation_manager.spawnPepper(
client_id,
spawn_ground_plane=True)
handle = pepper.subscribeCamera(PepperVirtual.ID_CAMERA_TOP)
#######################################################
# face detection & model prediction setup
#######################################################
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
model = models.load_model('model_keras')
#######################################################
# thread
#######################################################
if 2 <= len(sys.argv) :
if sys.argv[1] == "zemmour" :
search_for = sys.argv[1]
else :
search_for = "chalamet"
else :
search_for = "chalamet"
prediction_string = ""
img = pepper.getCameraFrame(handle)
state = "detection"
direction = "none"
try:
while True:
img = pepper.getCameraFrame(handle)
if state == "detection" :
detected_faces = face_cascade.detectMultiScale(image=img, scaleFactor=1.3, minNeighbors=6) #paramètres à modifier
if(len(detected_faces) > 0) :
for face in detected_faces :
(x, y, w, h) = face
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), thickness = 2)
prediction_string = model_prediction(img, x, y, w, h)
if prediction_string.split(" ")[0] == search_for :
if x-w/2 < 140 :
direction = "left"
if x+w/2 > 180 :
direction = "right"
else :
direction = "center"
state = "move"
cv2.putText(img, prediction_string, (x, y-12), cv2.FONT_HERSHEY_SIMPLEX, 0.4,(0, 255, 0),1)
cv2.imshow("where is "+search_for+" ?", img)
cv2.waitKey(1)
time.sleep(1)
if state == "move" :
if direction != "none" :
if direction == "left" :
pepper.moveTo(0,0,0.4)
if direction == "right" :
pepper.moveTo(0,0,-0.4)
pepper.moveTo(1,0,0)
state = "detection"
except KeyboardInterrupt:
simulation_manager.stopSimulation(client)