首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在后台调度程序运行时阻止程序崩溃?

如何在后台调度程序运行时阻止程序崩溃?
EN

Stack Overflow用户
提问于 2019-05-16 11:53:09
回答 1查看 186关注 0票数 0

我正在使用opencv绘制一张脸,然后在按下一个键时绘制另一张脸('s‘代表惊讶的脸,'d’代表悲伤的脸,'a‘代表愤怒,等等)。我想要我的脸模拟眼睛闪烁,所以我使用后台调度程序每五秒运行一次闪烁功能。闪烁函数是Blink类的一部分,它知道当前的脸是什么,所以当它“眨眼”时,它会重新绘制正确的脸。但是,每次我切换界面时,程序都会崩溃,我必须强制退出python。在我换脸之前,眨眼效果很好。

我试着在重新绘制新面孔之前暂停调度程序,然后在重新绘制之后恢复它,但这并没有什么不同。它仍然崩溃,我不知道还能尝试什么。该程序可以很好地切换脸部。只有当我添加后台调度程序和闪烁时,它才会崩溃。

代码语言:javascript
运行
复制
import numpy as np 
import cv2 as cv
from drawCopy import*
import random
from apscheduler.schedulers.background import BackgroundScheduler
import logging

class Blink:
    def __init__(self,currentFace,img,scheduler):
        self.currentFace = currentFace
        self.img = img
        self.scheduler = scheduler

    def blink(self):
        self.img[:] = backgroundColor
        blinkDict[self.currentFace](self.img)

    def addJob(self):
        self.blinkJob = self.scheduler.add_job(self.blink, 'interval', seconds=(random.uniform(3.0,5.0)),max_instances=2)

    def startSched(self):
        self.scheduler.start()

    def updateCurrentFace(self,newFace,newImg):
        self.currentFace = newFace
        self.img = newImg

blinkDict = {'n': blinkNeutral, 's': blinkSurprised, 'd': blinkSad, 'a': blinkAngry, 'h': blinkHappy}

logging.basicConfig()

img = np.zeros((600,1024,3), np.uint8)
img[:] = backgroundColor
cv.imshow('Face',img)

currentFace = cv.waitKey()
img = drawFace(img,chr(currentFace))
cv.imshow('Face',img)

scheduler = BackgroundScheduler()
blinkObj = Blink(chr(currentFace),img,scheduler)
blinkObj.addJob()
blinkObj.startSched()

#The code runs fine until here and then crashes when the next key is pressed

currentFace = cv.waitKey()
if currentFace == 27:
    cv.destroyAllWindows()
else:
    img[:] = backgroundColor
    img = drawFace(img,chr(currentFace))

    blinkObj.updateCurrentFace(chr(currentFace),img)

    cv.imshow('Face',img)
    cv.waitKey(0)

cv.destroyAllWindows()
EN

回答 1

Stack Overflow用户

发布于 2019-05-22 08:28:41

我在Linux机器上运行了相同的代码(之前已经在Macbook pro上运行过它),它工作得很好。我仍然不知道为什么它会崩溃,但这似乎是Mac或Macbook特有的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56160573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档