File size: 4,394 Bytes
5cc46fa 55c0335 5cc46fa |
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import cv2
import mediapipe as mp
import urllib.request
import numpy as np
import pickle
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import animation
# In[2]:
# In[3]:
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_holistic = mp.solutions.holistic
mp_pose = mp.solutions.pose
mp_face_mesh = mp.solutions.face_mesh
# In[4]:
face_url = "http://claireye.com.tw/img/20230222.jpg"
urllib.request.urlretrieve(face_url, "face_image.jpg")
# In[5]:
# Fetch image for analysis
img_url = "http://claireye.com.tw/img/230212a.jpg"
urllib.request.urlretrieve(img_url, "pose.jpg")
# In[6]:
import gradio as gr
# In[7]:
mp_selfie = mp.solutions.selfie_segmentation
# In[8]:
def segment(image):
with mp_selfie.SelfieSegmentation(model_selection=0) as model:
res = model.process(image)
mask = np.stack((res.segmentation_mask,)*3, axis=-1) > 0.5
return np.where(mask, image, cv2.blur(image, (40,40)))
# In[9]:
def facego(image):
with mp_face_mesh.FaceMesh(
static_image_mode=True,
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5) as face_mesh:
# Read image file with cv2 and convert from BGR to RGB
results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
annotated_image = image.copy()
for face_landmarks in results.multi_face_landmarks:
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_contours_style())
mp_drawing.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_IRISES,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_iris_connections_style())
return annotated_image
# In[10]:
def posego(image):
# Create a MediaPipe `Pose` object
with mp_pose.Pose(static_image_mode=True,
model_complexity=2,
enable_segmentation=True) as pose:
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Copy the iamge
annotated_image = image.copy()
# Draw pose, left and right hands, and face landmarks on the image with drawing specification defaults.
mp_drawing.draw_landmarks(annotated_image,
results.pose_landmarks,
mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
return annotated_image
# In[11]:
def inference(img, version):
print(version)
print("1")
print(img)
img2 = cv2.imread(img)
print("2")
print(img2)
if version == 'face':
img1=facego(img2)
print("1a")
elif (version == 'pose'):
img1=posego(img2)
print("2a")
else:
img1=segment(img2)
print("3a")
print("3")
print(img1)
save_path = f'out.jpg'
cv2.imwrite(save_path, img1)
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
return img1, save_path
# In[12]:
title = "pose-style"
description = "Gradio demo for pose-style. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
article = "<p style='text-align: center'><a href='http://claireye.com.tw'>Claireye</a> | 2023</p>"
# In[13]:
gr.Interface(
inference, [
gr.inputs.Image(type="filepath",label="Input"),
gr.inputs.Radio(['face', 'pose', 'seg'], type="value", default='pose', label='mode')
], [
gr.outputs.Image(type="numpy", label="Output (The whole image)"),
gr.outputs.File(label="Download the output image")
],
title=title,
description=description,
article=article,
examples=[['face_image.jpg', 'face'], ['pose.jpg', 'pose'],
['pose.jpg', 'seg']]).launch()
# In[ ]:
|