-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHumanInferCore.py
114 lines (95 loc) · 4.55 KB
/
HumanInferCore.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
# This Python file uses the following encoding: utf-8
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
import cv2
import torch
import subprocess
import logging
class HumanBodyInfer():
def __init__(self):
cache_path = "./cache"
if not os.path.exists(cache_path):
os.makedirs(cache_path)
logging.basicConfig(level=logging.INFO, # 设置日志级别
format='%(asctime)s - %(levelname)s - %(message)s',
filename='app.log', # 将日志写入文件
filemode='w')
self.logger = logging.getLogger()
def search_file(self, directory_path, key):
# 遍历目录,找出带有 '_rect.txt' 的文件
file = [os.path.abspath(os.path.join(directory_path, filename))
for filename in os.listdir(directory_path)
if filename.endswith(key)]
return file[0]
def getCachePath(self) -> str:
return os.path.dirname(os.path.realpath(__file__)) + "\cache"
def checkCudaIsAvailable(self) -> bool:
return torch.cuda.is_available()
def getObjPath(self) -> str:
return "file:///" + self.search_file(self.getCachePath(), "_mesh.obj")
def getPoseImagePath(self) -> str:
return "file:///" + self.search_file(self.getCachePath(), "_pose.png")
def getMeshImagePath(self) -> str:
return "file:///" + self.search_file(self.getCachePath(), "_mesh.png")
def checkExists(self, image_path):
if not os.path.exists(image_path):
return False
for file_name in os.listdir(self.getCachePath()):
file_path = os.path.join(self.getCachePath(), file_name)
# 判断是否为文件,如果是则删除
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Deleted file: {file_path}")
_, file_extension = os.path.splitext(image_path)
tmp_img = cv2.imread(image_path)
tmp_img_path = self.getCachePath() + "/" + os.path.basename(image_path)
cv2.imwrite(tmp_img_path, tmp_img)
return True
def getPoseRectTxt(self) -> bool:
files = os.listdir(self.getCachePath())
if (len(files) != 1):
return False
tmp_image_path = files[0]
cmd = r".\Python38\python.exe LightweightHumanPoseEstimation/run.py --image-path {}".format(
self.getCachePath() + "\\" + tmp_image_path)
self.logger.info(cmd)
try:
return_code = subprocess.call(cmd, shell=True)
if return_code != 0:
self.logger.error('getPoseRectTxt: {}'.format(subprocess.CalledProcessError(return_code, cmd)))
return False
self.logger.info("getPoseRectTxt: successful")
return True
except subprocess.CalledProcessError as e:
self.logger.error(f"getPoseRectTxt_1: Command failed with return code {e.returncode}")
self.logger.error(f"getPoseRectTxt_2: Error output:\n{e.output.decode('utf-8')}")
return False
def getMeshFile(self) -> bool:
files = os.listdir(self.getCachePath())
if (len(files) != 3):
self.logger.error("getMeshFile: 未生成依赖的三个文件")
return False
cmd = r".\Python38\python.exe Pifuhd/run.py --use_rect -i {} -o {}".format(self.getCachePath(),
self.getCachePath())
# cmd = r".\Python38\python.exe -m Pifuhd.apps.simple_test --use_rect -i {} -o {}".format(self.getCachePath(),
# self.getCachePath())
self.logger.info(cmd)
try:
return_code = subprocess.call(cmd, shell=True)
if return_code != 0:
self.logger.error('getMeshFile: {}'.format(subprocess.CalledProcessError(return_code, cmd)))
return False
self.logger.info("getMeshFile: successful")
return True
except subprocess.CalledProcessError as e:
self.logger.error(f"getMeshFile_1: Command failed with return code {e.returncode}")
self.logger.error(f"getMeshFile_2: Error output:\n{e.output.decode('utf-8')}")
return False
if __name__ == '__main__':
a = HumanBodyInfer()
print(a.checkExists(r"C:\Users\12168\Desktop\test_human_images\tmp2.png"))
print(a.getPoseRectTxt())
print(a.getMeshFile())
# print(a.getObjPath())
# print(a.getMeshImagePath())
# print(a.getPoseImagePath())