我写了一个图片处理的脚本,然后打包成exe文件,能正常运行,但是因为麻烦就想做成Winston服务,开机自启并且后台运行,可是在使用了nssm软件做成服务后,服务是开启了,但是没有正常运行,我图片都没有进行操作。想问下老师问题可能是哪里?我有用到多线程,生产者消费者模型,生产者是图片处理,消费者是移动图片,服务启动后只看到有图片处理结果,没有移动图片。。以下是主要代码:
def save_pic(self):
"""
保存图片
"""
while 1:
if self.savepath_queue.empty() == False:
get = self.savepath_queue.get()
if get == 1:
self.savepath_queue.task_done()
else:
img = get[0]
path = get[1]
hole_rate = get[2]
dir_path = get[3]
local = self.localpath + path
new = self.newpath + path
ok = self.ok + path
ng = self.ng + path
maked_path = dir_path+path
if hole_rate <= 17:
cv2.imwrite(local, img)
try:
if local:
move(local,new)
del local
del new
gc.collect()
except:
time.sleep(1)
move(local,new)
with open(self.logpath+"log.txt", "a+", encoding="utf-8") as f:
f.write('move(local,new)图档判定成功但是在移动文件时出错'+'\n')
f.write(traceback.format_exc())
# self.restart_program()
try:
if os.path.exists(maked_path):
move(maked_path,ok)
del maked_path
del ok
gc.collect()
except:
time.sleep(1)
move(maked_path,ok)
with open(self.logpath+"log.txt", "a+", encoding="utf-8") as f:
f.write('move(maked_path,ok)图档判定成功但是在移动文件时出错'+'\n')
f.write(traceback.format_exc())
# self.restart_program()
logText = time.strftime('%Y-%m-%d %H:%M:%S')+" "+ path
with open(self.logpath+"log.txt", "a+", encoding="utf-8") as f:
f.write(logText+' 空洞率合格!为'+str(hole_rate)[:5]+'%\n')
p = path.split('_')[0]
self.appendLog(typeID=p, rate=hole_rate)
elif hole_rate > 17:
cv2.imwrite(local, img)
try:
if os.path.exists(local):
move(local,new)
if os.path.exists(maked_path):
move(maked_path,ng)
except OSError as e:
# print("程序进行重启")
# self.restart_program()
pass
logText = time.strftime('%Y-%m-%d %H:%M:%S')+" "+ path
with open(self.logpath+"log.txt", "a", encoding="utf-8") as f:
f.write(logText+' 空洞率不合格!为'+str(hole_rate)[:6]+'%\n')
p = path.split('_')[0]
self.appendLog(typeID=p, rate=hole_rate)
self.savepath_queue.task_done()
def main(self):
import imghdr
while 1:
path_get = self.path_queue.get()
before_img = path_get[0] #文件名称
dir_path = path_get[1] #文件所在目录路径
# try:
# before1 = time.time()
print("正在处理",before_img)
cut_path = dir_path+before_img
wanzhengxing = self.is_valid_png(cut_path)
if wanzhengxing ==True:
# tracemalloc.start()
imgsize = os.path.getsize(cut_path)
if imgsize/1000 < 9000:
move(cut_path,self.failpath+before_img)
with open(self.logpath+"log.txt", "a+", encoding="utf-8") as f:
f.write(cut_path+"为错误图片,移除到Fail文件夹!\n")
p = before_img.split('_')[0]
self.appendLog(typeID=p)
self.savepath_queue.put(1,1,1)
self.path_queue.task_done()
else:
cut1 = self.picture_cut(cut_path)
maker = self.make(cut1) # 空洞为白色处理 清除较远和小的噪声
after = self.contour_approximate(maker, cut1) # 画出空洞部分呈现黑色矩形
clear = self.clear_noise(after) # 构造出白色背景
test = self.back_and_fore(clear, maker) # 将前后目标区域相加 突出空洞区
img = self.contour_empty(test, cut1) # 在原图对空洞区域画框
back_count = cv2.countNonZero(clear)
fore_count = cv2.countNonZero(test)
hole_rate = (fore_count / back_count) * 100
cv2.putText(img, "hole_rate = {:.5f}%".format(hole_rate), (200, 200), cv2.FONT_HERSHEY_COMPLEX, 2,
(255, 0, 0), 2)
contour = self.out_contour(clear, cut1)
del cut1
gc.collect()
ROI = cv2.bitwise_and(img, contour) # oriimg是原始图片 做与运算
# after = time.time() - before1
# cv2.putText(ROI, "run_time = {:.3f}s".format(after), (200, 300), cv2.FONT_HERSHEY_COMPLEX, 2,
# (255, 0, 0), 2)
self.savepath_queue.put([ROI, before_img, hole_rate, dir_path])
self.path_queue.task_done()
# _flask = tracemalloc.take_snapshot()
# _top = _flask.statistics('filename')
# for top in _top[:1]:
# print(top)
elif wanzhengxing == False:
time.sleep(2)
i=5
with open(self.logpath+"log.txt", "a+", encoding="utf-8") as f:
f.write(f"{cut_path}图片不完整,等待2s再处理!\n")
with open(self.logpath+"log.txt","a+",encoding='utf-8') as f:
for line in f:
o = "{}图片不完整,等待2s再处理!\n".format(cut_path)
if line == o:
i-=1
if i==0:
try:
move(cut_path,self.failpath+before_img)
with open(self.logpath+"log.txt", "a+", encoding="utf-8") as f:
f.write(cut_path+"处理次数超过次数,移除到Fail文件夹!\n")
p = before_img.split('_')[0]
self.appendLog(typeID=p)
except FileNotFoundError as e:
with open(self.logpath+"log.txt", "a+", encoding="utf-8") as f:
f.write(str(e)+"文件不存在,已经被移除!\n")
# self.restart_program()
self.savepath_queue.put(1,1,1)
self.path_queue.task_done()
def run(self):
thread_list = []
self.while_get_photo()
a= len(threading.enumerate())
for i in range(2):
if a > 17:
continue
else:
t_path_main = threading.Thread(target=self.main)
thread_list.append(t_path_main)
for i in range(7):
if a > 14:
continue
else:
t_path_save = threading.Thread(target=self.save_pic)
thread_list.append(t_path_save)
for t in thread_list:
t.setDaemon(True)
t.start()
for j in [self.path_queue, self.savepath_queue]:
j.join()
if __name__ == "__main__":
chengxu = VoidRatioTreatment()
while 1:
chengxu.run()
带你彻底掌握Scrapy,用Django+Elasticsearch搭建搜索引擎
了解课程