Rezilin
Posts: 3
Joined: Tue Mar 12, 2013 4:59 pm
Location: New York

Accessing a picture file while its being updated

Fri Mar 29, 2013 1:48 am

Greetings,

I am creating a jpeg camera based algorithm for a robots eye which takes a 320X240 jpg image saves it under the name photo.jpg, fromn here I use mahotas to read in the file with imread and preform a threashold/binary conversion with pymorph to create a binary array to be used in a line tracking algorithm. I am looping this all in a for loop which imcrements the binay save jpg name. Then the camera takes another photo again with the file name photo.jpg to converse memory and the pocess repeats. The problem im having is the photo.jpg is already opened with imread so I belive it isn't being updated in the code but outside it so to speak. Is there a way to close the imread take a new picture open it, and repeat the file update each time or to create a overwrite for the camera. I have programming experience but am fairly new to python and its file methodology so I will thank anyone who might enlighten me on this.

Thanks for your time,
Rezilin

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Accessing a picture file while its being updated

Sun Mar 31, 2013 12:18 pm

Does the image need to be saved to disk/card? Can you not use a webcam and do everything in memory using something like imageproc or PyGame?
It would simplify things enormously.

Dave.
Apple say... Monkey do !!

gordon77
Posts: 5075
Joined: Sun Aug 05, 2012 3:12 pm

Re: Accessing a picture file while its being updated

Sun Mar 31, 2013 9:48 pm

This program may help you with using a webcam and converting to strings and arrays to avoid storing them ,same as you I started with the save to file method but found this much quicker

Gordon77

Code: Select all

#!/usr/bin/python

import pygame, sys
from pygame.locals import *
import pygame.camera
import numpy
import time

black = 0,0,0

pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(352,288))
cam.start()
time.sleep(1)

windowSurfaceObj = pygame.display.set_mode((352,288))

# take first picture       
image1 = cam.get_image()

#save if required
#pygame.image.save(image1,'11.jpg')

# convert to a string
strim1 = pygame.image.tostring(image1,"RGB",1)

# display first picture from string
image = pygame.image.fromstring(strim1,(352,288),"RGB",1) 
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()
time.sleep(2)
windowSurfaceObj.fill(black)
pygame.display.update()

# take second picture
image2 = cam.get_image()

#save if required
#pygame.image.save(image2,'12.jpg')

# convert to a string
strim2 = pygame.image.tostring(image2,"RGB",1)

# display second picture from string
image = pygame.image.fromstring(strim2,(352,288),"RGB",1)
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()
time.sleep(2)
windowSurfaceObj.fill(black)
pygame.display.update()

mx = []
my = []
# convert first picture from string to a numpy.array
mx = numpy.fromstring(strim1, dtype='uint8')

# convert second picture from string to a numpy.array
my = numpy.fromstring(strim2, dtype='uint8')

# add both pictures together, average
strim3 = (mx/2) + (my/2)

# convert resultant numpy.array to a string
output = numpy.ndarray.tostring(strim3)

# display output picture from string   
image3 = pygame.image.fromstring(output,(352,288),"RGB",1)

#save if required
pygame.image.save(image3,'13.jpg')

catSurfaceObj = image3
windowSurfaceObj.blit(catSurfaceObj,(1,1))
pygame.display.update()

Rezilin
Posts: 3
Joined: Tue Mar 12, 2013 4:59 pm
Location: New York

Re: Accessing a picture file while its being updated

Mon Apr 01, 2013 7:12 pm

I actually fixed it I read the camera data in but it wasn't resetting my camera so by calling a function that I missed I am able to save multiple images. Taking raw data from the camera would be more memory efficient but since i'm saving over the same image and taking data from them as a numpy array I think it is ok, plus it lets me see the rate at which the photo's update per movement of the actual robotics platform.

Thanks for your responses,

Return to “Python”