Note that there are some explanatory texts on larger screens.

plurals
  1. POConcurrent functions running in separate process using pygame and multiprocessing
    primarykey
    data
    text
    <p>Suppose we want to drive an autonomous car by predicting image labels from a previous set of images and labels collected (A Machine Learning application). For this task, the car is connected via bluetooth serial (rfcomm) to the Host Computer (A PC with *NIX) and the images are streamed directly from an Android phone using IP Webcam, meanwhile, the PC is running a program that links this two functions, displaying the captured images in a drawing environment created by <code>pygame</code>, and sending the instructions back to the car using serial.</p> <p>At the moment, I've tried to implement those processes using the <code>multiprocessing</code> module, the seemed to work, but when I execute the client, the drawing function (<code>if __name__ == '__main__'</code>) works after the <code>getKeyPress()</code> function ends.</p> <p>The question is: It is possible to parallelize or synchronize the drawing fuinction enclosed within the <code>if __name__ == '__main__'</code> with the process declared in <code>getKyPress()</code>, such that the program works in two independent processes? </p> <p>Here's the implemented code so far:</p> <pre><code>import urllib import time import os import sys import serial import signal import multiprocessing import numpy as np import scipy import scipy.io as sio import matplotlib.image as mpimg from pygame.locals import * PORT = '/dev/rfcomm0' SPEED = 115200 ser = serial.Serial(PORT) status = False move = None targets = [] inputs = [] tic = False def getKeyPress(): import pygame pygame.init() global targets global status while not status: pygame.event.pump() keys = pygame.key.get_pressed() targets, status = processOutputs(targets, keys) targets = np.array(targets) targets = flattenMatrix(targets) sio.savemat('targets.mat', {'targets':targets}) def rgb2gray(rgb): r, g, b = np.rollaxis(rgb[...,:3], axis = -1) return 0.299 * r + 0.587 * g + 0.114 * b def processImages(inputX, inputs): inputX = flattenMatrix(inputX) if len(inputs) == 0: inputs = inputX elif inputs.shape[1] &gt;= 1: inputs = np.hstack((inputs, inputX)) return inputs def flattenMatrix(mat): mat = mat.flatten(1) mat = mat.reshape((len(mat), 1)) return mat def send_command(val): connection = serial.Serial( PORT, SPEED, timeout=0, stopbits=serial.STOPBITS_TWO ) connection.write(val) connection.close() def processOutputs(targets, keys): global move global status global tic status = False keypress = ['K_p', 'K_UP', 'K_LEFT', 'K_DOWN', 'K_RIGHT'] labels = [1, 2, 3, 4, 5] commands = ['p', 'w', 'r', 'j', 's'] text = ['S', 'Up', 'Left', 'Down', 'Right'] if keys[K_q]: status = True return targets, status else: for i, j, k, g in zip(keypress, labels, commands, text): cmd = compile('cond = keys['+i+']', '&lt;string&gt;', 'exec') exec cmd if cond: move = g targets.append(j) send_command(k) break send_command('p') return targets, status targetProcess = multiprocessing.Process(target=getKeyPress) targetProcess.daemon = True targetProcess.start() if __name__ == '__main__': import pygame pygame.init() w = 288 h = 352 size=(w,h) screen = pygame.display.set_mode(size) c = pygame.time.Clock() # create a clock object for timing pygame.display.set_caption('Driver') ubuntu = pygame.font.match_font('Ubuntu') font = pygame.font.Font(ubuntu, 13) inputs = [] try: while not status: urllib.urlretrieve("http://192.168.0.10:8080/shot.jpg", "input.jpg") try: inputX = mpimg.imread('input.jpg') except IOError: status = True inputX = rgb2gray(inputX)/255 out = inputX.copy() out = scipy.misc.imresize(out, (352, 288), interp='bicubic', mode=None) scipy.misc.imsave('input.png', out) inputs = processImages(inputX, inputs) print inputs.shape[1] img=pygame.image.load('input.png') screen.blit(img,(0,0)) pygame.display.flip() c.tick(1) if move != None: text = font.render(move, False, (255, 128, 255), (0, 0, 0)) textRect = text.get_rect() textRect.centerx = 20 #screen.get_rect().centerx textRect.centery = 20 #screen.get_rect().centery screen.blit(text, textRect) pygame.display.update() if status: targetProcess.join() sio.savemat('inputs.mat', {'inputs':inputs}) except KeyboardInterrupt: targetProcess.join() sio.savemat('inputs.mat', {'inputs':inputs}) targetProcess.join() sio.savemat('inputs.mat', {'inputs':inputs}) </code></pre> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload