程序优化
This commit is contained in:
1 parent
e83deda268
commit
e1a7647fcb
16 files changed
+634
-91
No files matched your search
+163
-8
@@ -1,5 +1,8 @@
|
||||
import redis,sys,os,re,oss2,shutil,time
|
||||
import redis,sys,os,re,oss2,shutil,time,cv2
|
||||
import json
|
||||
import requests
|
||||
import platform
|
||||
import numpy as np
|
||||
import xml.etree.ElementTree as ET
|
||||
from PIL import ImageGrab
|
||||
if platform.system() == 'Windows':
|
||||
@@ -222,14 +225,14 @@ def uploadControlPointsOss(pid):
|
||||
#截屏保存
|
||||
def saveScreenImg(pid):
|
||||
#获取当前的日志
|
||||
if not os.path.exists(os.path.join(config.workdir,"screen", time.strftime("%y%m%d" time.localtime()))):
|
||||
os.makedirs(os.path.join(config.workdir,"screen", time.strftime("%y%m%d" time.localtime())))
|
||||
if not os.path.exists(os.path.join(config.workdir,"screen", time.strftime("%y%m%d",time.localtime()))):
|
||||
os.makedirs(os.path.join(config.workdir,"screen", time.strftime("%y%m%d",time.localtime())))
|
||||
screenshot = ImageGrab .grab()
|
||||
screenshot.save(os.path.join(config.workdir,"screen", time.strftime("%y%m%d" time.localtime())),str(pid)+".png")
|
||||
screenshot.save(os.path.join(config.workdir,"screen", time.strftime("%y%m%d",time.localtime()))+"/"+str(pid)+".png")
|
||||
#移动到e盘
|
||||
if not os.path.exists(os.path.join(config.sharedir,"screen", time.strftime("%y%m%d" time.localtime()))):
|
||||
os.makedirs(os.path.join(config.sharedir,"screen", time.strftime("%y%m%d" time.localtime())))
|
||||
shutil.copytree(os.path.join(config.workdir,"screen", time.strftime("%y%m%d" time.localtime())), os.path.join(config.sharedir,"screen", time.strftime("%y%m%d" time.localtime())))
|
||||
if not os.path.exists(os.path.join(config.sharedir,"screen", time.strftime("%y%m%d",time.localtime()))):
|
||||
os.makedirs(os.path.join(config.sharedir,"screen", time.strftime("%y%m%d",time.localtime())))
|
||||
shutil.copy(os.path.join(config.workdir,"screen", time.strftime("%y%m%d",time.localtime()))+"\\"+str(pid)+".png", os.path.join(config.sharedir,"screen", time.strftime("%y%m%d",time.localtime())))
|
||||
|
||||
|
||||
#文件夹的移动和删除
|
||||
@@ -247,4 +250,156 @@ def removeFolder(pid):
|
||||
file_time = os.path.getmtime(os.path.join(config.workdir, 'finished', file))
|
||||
now_time = time.time()
|
||||
if (now_time - file_time) > 259200:
|
||||
shutil.rmtree(os.path.join(config.workdir, 'finished', file), ignore_errors=True)
|
||||
shutil.rmtree(os.path.join(config.workdir, 'finished', file), ignore_errors=True)
|
||||
|
||||
|
||||
def find_last_x(image, slope_threshold = 1000):
|
||||
x = []
|
||||
y = []
|
||||
hist, bins = np.histogram(image, bins=256, range=[0, 256])
|
||||
|
||||
#找到50以内的最高峰
|
||||
max_y = 0
|
||||
max_i = 5
|
||||
for i in range(5, 50):
|
||||
if hist[i] > max_y:
|
||||
max_y = hist[i]
|
||||
max_i = i
|
||||
print(f'50以内最高峰值y:{max_y},最高峰位置x:{max_i}')
|
||||
|
||||
for i in range(2, max_i):
|
||||
x.append(i)
|
||||
y.append(hist[i])
|
||||
slopes = [abs(y[i + 1] - y[i]) for i in range(len(x) - 1)]
|
||||
|
||||
current_interval = []
|
||||
max_interval = []
|
||||
max_x = {}
|
||||
for i, slope in enumerate(slopes):
|
||||
current_interval.append(slope)
|
||||
if slope >= slope_threshold:
|
||||
if len(current_interval) > len(max_interval):
|
||||
max_interval = current_interval.copy()
|
||||
max_x[x[i]] = slope
|
||||
current_interval = []
|
||||
|
||||
print(max_x)
|
||||
last_x = list(max_x)[-1]
|
||||
last_y = max_x[last_x]
|
||||
return last_x, last_y
|
||||
|
||||
def high_find_histogram_range(image, target_frequency):
|
||||
'''
|
||||
循环查找在 target_frequency (y)频次限制下的直方图区间值(x)
|
||||
:param image: 导入图片
|
||||
:param target_frequency: 直方图 y 频次限制条件
|
||||
:return: 直方图区间 x,和 该区间频次 y
|
||||
'''
|
||||
# 计算灰度直方图
|
||||
hist, bins = np.histogram(image, bins=256, range=[0, 256])
|
||||
# 初始化区间和频次
|
||||
interval = 255
|
||||
frequency = hist[255]
|
||||
while frequency < target_frequency:
|
||||
# 更新区间和频次
|
||||
interval -= 1
|
||||
# 检查直方图的频次是否为None,如果频次是None,则将其设为0,这样可以避免将None和int进行比较报错。
|
||||
frequency = hist[interval] if hist[interval] is not None else 0
|
||||
frequency += hist[interval] if hist[interval] is not None else 0
|
||||
# 如果频次接近10000则停止循环
|
||||
if target_frequency - 2000 <= frequency <= target_frequency + 2000:
|
||||
break
|
||||
|
||||
return interval, frequency
|
||||
|
||||
def ps_color_scale_adjustment(image, shadow=0, highlight=255, midtones=1):
|
||||
'''
|
||||
模拟 PS 的色阶调整; 0 <= Shadow < Highlight <= 255
|
||||
:param image: 传入的图片
|
||||
:param shadow: 黑场(0-Highlight)
|
||||
:param highlight: 白场(Shadow-255)
|
||||
:param midtones: 灰场(9.99-0.01)
|
||||
:return: 图片
|
||||
'''
|
||||
if highlight > 255:
|
||||
highlight = 255
|
||||
if shadow < 0:
|
||||
shadow = 0
|
||||
if shadow >= highlight:
|
||||
shadow = highlight - 2
|
||||
if midtones > 9.99:
|
||||
midtones = 9.99
|
||||
if midtones < 0.01:
|
||||
midtones = 0.01
|
||||
image = np.array(image, dtype=np.float16)
|
||||
# 计算白场 黑场离差
|
||||
Diff = highlight - shadow
|
||||
image = image - shadow
|
||||
image[image < 0] = 0
|
||||
image = (image / Diff) ** (1 / midtones) * 255
|
||||
image[image > 255] = 255
|
||||
image = np.array(image, dtype=np.uint8)
|
||||
|
||||
return image
|
||||
|
||||
|
||||
|
||||
def remove_gray_and_sharpening(jpg_path):
|
||||
#low_y_limit = 25000
|
||||
high_y_limit = 13000
|
||||
input_image = cv2.imread(jpg_path)
|
||||
# low_x_thresh, low_y_frequency = low_find_histogram_range(input_image, low_y_limit)
|
||||
low_x_thresh, low_y_frequency = find_last_x(input_image, 1000)
|
||||
high_x_thresh, high_y_frequency = high_find_histogram_range(input_image, high_y_limit)
|
||||
print(f"{low_x_thresh} 区间, {low_y_frequency} 频次")
|
||||
print(f"{high_x_thresh} 区间, {high_y_frequency} 频次")
|
||||
high_output_image = ps_color_scale_adjustment(input_image, shadow=low_x_thresh, highlight=high_x_thresh, midtones=1)
|
||||
cv2.imwrite(jpg_path, high_output_image, [cv2.IMWRITE_JPEG_QUALITY, 95]) # 保存图片的质量是原图的 95%
|
||||
|
||||
#读取指定路径判断是否对齐成功
|
||||
def isAlignNums(strPid):
|
||||
#拼接路径
|
||||
csvPath = os.path.join(config.workdir, strPid, strPid+"_align.csv")
|
||||
print(csvPath)
|
||||
#判断文件是否存在
|
||||
if os.path.exists(csvPath) == False:
|
||||
return "rebuild"
|
||||
#读取文件行数
|
||||
lines = ""
|
||||
with open(csvPath, 'r') as f:
|
||||
lines = f.readlines()
|
||||
#获取长度
|
||||
lines = len(lines) - 1
|
||||
#获取pid对应的 photo1 和 photo2 的数量
|
||||
photo1Num = 0
|
||||
photo2Num = 0
|
||||
for file in os.listdir(os.path.join(config.workdir, strPid, 'photo1')):
|
||||
if file.endswith('.jpg'):
|
||||
photo1Num += 1
|
||||
|
||||
for file in os.listdir(os.path.join(config.workdir, strPid, 'photo2')):
|
||||
if file.endswith('.jpg'):
|
||||
photo2Num += 1
|
||||
#获取图片总数
|
||||
totalPhotos = photo1Num + photo2Num
|
||||
#比对对齐数量
|
||||
if totalPhotos - lines >= 4:
|
||||
return "rebuild"
|
||||
|
||||
return True
|
||||
|
||||
|
||||
#消息通知
|
||||
def notify(content):
|
||||
|
||||
if content == "":
|
||||
return "content 不能为空"
|
||||
|
||||
for user_agent_id in config.notify_user_Ids:
|
||||
data = {
|
||||
'userId': user_agent_id,
|
||||
'message': content,
|
||||
}
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
message_send_url = "https://mp.api.suwa3d.com/api/qyNotify/sendMessage?userId="+user_agent_id+"&message="+content
|
||||
response = requests.post(message_send_url, data=json.dumps(data), headers=headers)
|
||||
Reference in new issue
Block a user