Advertisement
Guest User

Mini-curso de reconhecimento facial

a guest
Oct 23rd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Mini-curso de reconhecimento facial"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 5,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "from PIL import Image, ImageDraw\n",
  17. "import face_recognition as fr\n",
  18. "import numpy as np"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": 71,
  24. "metadata": {},
  25. "outputs": [],
  26. "source": [
  27. "rosto_1 = fr.load_image_file('giovana.jpeg')\n",
  28. "rosto_2 = fr.load_image_file('luiz.jpeg')\n",
  29. "rosto_3 = fr.load_image_file('ambos.jpg')"
  30. ]
  31. },
  32. {
  33. "cell_type": "code",
  34. "execution_count": 11,
  35. "metadata": {},
  36. "outputs": [
  37. {
  38. "data": {
  39. "text/plain": [
  40. "(800, 601, 3)"
  41. ]
  42. },
  43. "execution_count": 11,
  44. "metadata": {},
  45. "output_type": "execute_result"
  46. }
  47. ],
  48. "source": [
  49. "rosto_1.shape"
  50. ]
  51. },
  52. {
  53. "cell_type": "code",
  54. "execution_count": 12,
  55. "metadata": {},
  56. "outputs": [
  57. {
  58. "data": {
  59. "text/plain": [
  60. "(460, 460, 3)"
  61. ]
  62. },
  63. "execution_count": 12,
  64. "metadata": {},
  65. "output_type": "execute_result"
  66. }
  67. ],
  68. "source": [
  69. "rosto_2.shape"
  70. ]
  71. },
  72. {
  73. "cell_type": "code",
  74. "execution_count": 13,
  75. "metadata": {},
  76. "outputs": [
  77. {
  78. "data": {
  79. "text/plain": [
  80. "(1280, 606, 3)"
  81. ]
  82. },
  83. "execution_count": 13,
  84. "metadata": {},
  85. "output_type": "execute_result"
  86. }
  87. ],
  88. "source": [
  89. "rosto_3.shape"
  90. ]
  91. },
  92. {
  93. "cell_type": "code",
  94. "execution_count": 14,
  95. "metadata": {},
  96. "outputs": [],
  97. "source": [
  98. "rosto_1_pil = Image.open('giovana.jpeg')"
  99. ]
  100. },
  101. {
  102. "cell_type": "code",
  103. "execution_count": 17,
  104. "metadata": {},
  105. "outputs": [],
  106. "source": [
  107. "localizacao_1 = fr.face_locations(rosto_1)[0]"
  108. ]
  109. },
  110. {
  111. "cell_type": "code",
  112. "execution_count": 18,
  113. "metadata": {},
  114. "outputs": [],
  115. "source": [
  116. "cima, direita, baixo, esquerda = localizacao_1"
  117. ]
  118. },
  119. {
  120. "cell_type": "code",
  121. "execution_count": 19,
  122. "metadata": {},
  123. "outputs": [],
  124. "source": [
  125. "rosto_1_draw = ImageDraw.Draw(rosto_1_pil)"
  126. ]
  127. },
  128. {
  129. "cell_type": "code",
  130. "execution_count": 20,
  131. "metadata": {},
  132. "outputs": [],
  133. "source": [
  134. "rosto_1_draw.rectangle([esquerda,cima,direita,baixo])"
  135. ]
  136. },
  137. {
  138. "cell_type": "code",
  139. "execution_count": 39,
  140. "metadata": {},
  141. "outputs": [],
  142. "source": [
  143. "#rosto_1_pil"
  144. ]
  145. },
  146. {
  147. "cell_type": "code",
  148. "execution_count": 30,
  149. "metadata": {},
  150. "outputs": [],
  151. "source": [
  152. "crop_1 = rosto_1_pil.crop([esquerda, cima, direita, baixo])"
  153. ]
  154. },
  155. {
  156. "cell_type": "code",
  157. "execution_count": 35,
  158. "metadata": {},
  159. "outputs": [],
  160. "source": [
  161. "rosto_1 = rosto_1[cima:baixo,esquerda:direita]"
  162. ]
  163. },
  164. {
  165. "cell_type": "code",
  166. "execution_count": 36,
  167. "metadata": {},
  168. "outputs": [
  169. {
  170. "data": {
  171. "text/plain": [
  172. "(268, 268, 3)"
  173. ]
  174. },
  175. "execution_count": 36,
  176. "metadata": {},
  177. "output_type": "execute_result"
  178. }
  179. ],
  180. "source": [
  181. "rosto_1.shape"
  182. ]
  183. },
  184. {
  185. "cell_type": "code",
  186. "execution_count": 44,
  187. "metadata": {},
  188. "outputs": [],
  189. "source": [
  190. "atributos_1 = np.stack(fr.face_encodings(rosto_1))"
  191. ]
  192. },
  193. {
  194. "cell_type": "code",
  195. "execution_count": 48,
  196. "metadata": {},
  197. "outputs": [],
  198. "source": [
  199. "atributos_2 = np.stack(fr.face_encodings(rosto_2))"
  200. ]
  201. },
  202. {
  203. "cell_type": "code",
  204. "execution_count": 49,
  205. "metadata": {},
  206. "outputs": [],
  207. "source": [
  208. "atributos_3 = np.stack(fr.face_encodings(rosto_3))"
  209. ]
  210. },
  211. {
  212. "cell_type": "code",
  213. "execution_count": 46,
  214. "metadata": {},
  215. "outputs": [
  216. {
  217. "data": {
  218. "text/plain": [
  219. "[False]"
  220. ]
  221. },
  222. "execution_count": 46,
  223. "metadata": {},
  224. "output_type": "execute_result"
  225. }
  226. ],
  227. "source": [
  228. "fr.compare_faces(atributos_1, atributos_2)"
  229. ]
  230. },
  231. {
  232. "cell_type": "code",
  233. "execution_count": 47,
  234. "metadata": {},
  235. "outputs": [
  236. {
  237. "data": {
  238. "text/plain": [
  239. "array([0.79240812])"
  240. ]
  241. },
  242. "execution_count": 47,
  243. "metadata": {},
  244. "output_type": "execute_result"
  245. }
  246. ],
  247. "source": [
  248. "fr.face_distance(atributos_1, atributos_2)"
  249. ]
  250. },
  251. {
  252. "cell_type": "code",
  253. "execution_count": 55,
  254. "metadata": {},
  255. "outputs": [
  256. {
  257. "data": {
  258. "text/plain": [
  259. "[False, True]"
  260. ]
  261. },
  262. "execution_count": 55,
  263. "metadata": {},
  264. "output_type": "execute_result"
  265. }
  266. ],
  267. "source": [
  268. "fr.compare_faces(atributos_2, atributos_3)"
  269. ]
  270. },
  271. {
  272. "cell_type": "code",
  273. "execution_count": 72,
  274. "metadata": {},
  275. "outputs": [],
  276. "source": [
  277. "marcas = fr.face_landmarks(rosto_1)[0]"
  278. ]
  279. },
  280. {
  281. "cell_type": "code",
  282. "execution_count": 62,
  283. "metadata": {},
  284. "outputs": [
  285. {
  286. "data": {
  287. "text/plain": [
  288. "dict"
  289. ]
  290. },
  291. "execution_count": 62,
  292. "metadata": {},
  293. "output_type": "execute_result"
  294. }
  295. ],
  296. "source": [
  297. "type(marcas)"
  298. ]
  299. },
  300. {
  301. "cell_type": "code",
  302. "execution_count": 57,
  303. "metadata": {},
  304. "outputs": [
  305. {
  306. "data": {
  307. "text/plain": [
  308. "<PIL.ImageDraw.ImageDraw at 0x7f50f5dd14e0>"
  309. ]
  310. },
  311. "execution_count": 57,
  312. "metadata": {},
  313. "output_type": "execute_result"
  314. }
  315. ],
  316. "source": [
  317. "rosto_1_draw"
  318. ]
  319. },
  320. {
  321. "cell_type": "code",
  322. "execution_count": 73,
  323. "metadata": {},
  324. "outputs": [],
  325. "source": [
  326. "for locations in marcas.keys():\n",
  327. " rosto_1_draw.point(marcas[locations])"
  328. ]
  329. },
  330. {
  331. "cell_type": "code",
  332. "execution_count": 76,
  333. "metadata": {},
  334. "outputs": [],
  335. "source": [
  336. "#rosto_1_pil"
  337. ]
  338. },
  339. {
  340. "cell_type": "markdown",
  341. "metadata": {},
  342. "source": [
  343. "site: pyimagesearch"
  344. ]
  345. }
  346. ],
  347. "metadata": {
  348. "kernelspec": {
  349. "display_name": "Python 3",
  350. "language": "python",
  351. "name": "python3"
  352. },
  353. "language_info": {
  354. "codemirror_mode": {
  355. "name": "ipython",
  356. "version": 3
  357. },
  358. "file_extension": ".py",
  359. "mimetype": "text/x-python",
  360. "name": "python",
  361. "nbconvert_exporter": "python",
  362. "pygments_lexer": "ipython3",
  363. "version": "3.6.8"
  364. }
  365. },
  366. "nbformat": 4,
  367. "nbformat_minor": 2
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement