使用python

时间:2019-09-14 06:29:45

标签: python python-multiprocessing python-multithreading

我正在尝试在树莓派上创建一个应用,该应用在倒数计时后拍摄3张照片。拍摄完照片后,它将调整大小并将其放入模板中。我相信我没有正确设置多进程,并且经过几次运行后,相机拍照之前会有一个延迟,因为它正在等待将3张照片放置在模板中。任何建议或帮助都很好

import tkinter as tk
import time
import threading
from multiprocessing import Pool
import queue
import cameraService
import imageService

def testPhotobooth(images):  
        templatePath = '/home/pi/Documents/Test/template0.jpg'
        destinationPath = '/home/pi/Documents/Test/testPhotobooth1.jpg'
        service = imageService.ImageService()
        service.createPhotoboothImage(images, templatePath, destinationPath)

class GUIApp:
    def __init__(self):
        self.root = tk.Tk()
        self.root.attributes('-zoomed', True)
        self.buttonCountDown = tk.Button(text='Count Down', command=self.countDownAction)
        self.buttonCountDown.pack()
        self.label = tk.Label(text='Ready!', bg='#3D434F', fg='white', font=('Helvetica', 100, 'bold'))
        self.label.pack(side='top', fill='both', expand=True)
        self.queue = queue.Queue()
        self.captureQueue = queue.Queue()
        self.imageProcessQueue = Queue()
        self.imageService = imageService.ImageService()
        self.cameraService = cameraService.CameraService()
        self.imageCount = 0
        self.images = []
        self.pool = Pool(4)
        threading.Thread(target=self.listenToCaptureQueue).start()               
        threading.Thread(target=self.listenToQueue).start()        
        threading.Thread(target=self.listenToImageProcessQueue).start()
        self.root.mainloop()

    def countDown(self, seconds):
        for i in range(seconds, 0, -1):
            self.queue.put(i)
            time.sleep(1)
        self.queue.put('SMILE!')
        time.sleep(1)
        self.captureQueue.put(True)

    def listenToQueue(self):
        while True:
            try:
                if self.queue.empty() == False:
                    s = self.queue.get(0)
                    self.label['text'] = s
                elif self.queue.empty() == True: 
                    pass
            except queue.Empty:
                pass

    def listenToCaptureQueue(self):
        while True:
            try:
                if self.captureQueue.empty() == False:
                    if self.captureQueue.get(0):
                        self.label['text'] = 'Please wait...'
                        filePath = self.cameraService.captureImage()
                        self.images.append("%r"%filePath)
                        self.imageCount += 1
                        if self.imageCount < 3:
                            threading.Thread(target=self.countDown, args=(3,)).start()
                        else:
                            self.buttonCountDown['state'] = 'normal'  
                            self.label['text'] = 'Ready!'
                            self.imageProcessQueue.put(self.images)
                elif self.captureQueue.empty() == True:
                    pass
            except queue.Empty:
                pass

    def listenToImageProcessQueue(self):
        while True:
            try:
                if self.imageProcessQueue.empty() == False:                   
                        images = self.imageProcessQueue.get(0)
                        self.pool.apply_async(testPhotobooth, [images])
                elif self.imageProcessQueue.empty() == True:
                    pass
            except queue.Empty:
                pass

    def countDownAction(self):
        self.images = []
        self.imageCount = 0
        self.buttonCountDown['state'] = 'disabled'         
        threading.Thread(target=self.countDown, args=(5,)).start()

app = GUIApp()

0 个答案:

没有答案