Commit a1ca2aa6 by Benjamin Gallois

Initial commit

parent b7a54dc7
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
import numpy as np
import pygame
import pygame.camera
from pygame.locals import *
import time
from trackingPython import *
import cv2
class Window(QtWidgets.QWidget):
def __init__(self):
# Init window
super().__init__()
self.title = 'Live camera'
self.left = 10
self.top = 10
self.width = 1200
self.height = 600
self.setupUi()
# Init camera
pygame.init()
pygame.camera.init()
self.cam = pygame.camera.Camera("/dev/video1", (5000, 5000)) # Camera, change videoX to change camera
self.cam.start()
self.size = self.cam.get_size()
self.image = pygame.surface.Surface(self.size)
#self.background = self.cam.get_image()
#self.background = pygame.transform.flip(self.background, True, True)
#self.background = pygame.surfarray.array2d(self.background).astype(np.uint8)
def setupUi(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.im_widget = pg.ImageView(self) # Live camera feeds
self.im_widget.ui.histogram.hide()
self.im_widget2 = pg.PlotWidget(self) # Live plot
self.im_widget2.setXRange(0, 100, padding=0)
self.im_widget2.setYRange(-100, 0, padding=0)
self.im_widget2.move(550, 0)
self.initialisationFigure()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.majFigure)
self.timer.start(16)
self.timer2 = QtCore.QTimer(self)
self.timer2.timeout.connect(self.NumberRefreshPerSecond)
self.timer2.start(1000)
def NumberRefreshPerSecond(self):
print(self.count)
self.count = 0
def majFigure(self):
self.count = self.count + 1
if self.cam.query_image():
#Acquisition
self.image = self.cam.get_image(self.image)
#self.image = pygame.transform.flip(self.image, True, True)
self.img = pygame.surfarray.array2d(self.image).astype(np.uint8)
#self.img = cv2.absdiff(self.img, self.background)
# Binarisation
self.ret, self.imgf = cv2.threshold(self.img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#ell = ellipse(self.imgf)
'''# Analysis
self.para = ell.parameter()
self.x = self.para[0]
self.y = self.para[1]'''
self.im_widget.setImage(self.imgf)
'''self.im_widget2.clear()
self.im_widget2.plot([self.y], [-self.x], symbol='o') # setImage display image with a rotation of pi/2
self.im_widget2.resize(550,590)'''
def initialisationFigure(self):
self.count = 0
self.im_widget.show()
self.im_widget2.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = Window()
ex.show()
sys.exit(app.exec_())
import numpy as np
from scipy import stats
from scipy.misc import imread, imsave
import matplotlib.pyplot as plt
import scipy.ndimage
import glob
import matplotlib.animation as animation
class ellipse:
"""
A simple class to track an object with moments of an image (ellipse method).
Tracks the center of mass, the orientation and the direction.
Parameters
----------
img : 2D array
The binary image where the object to track is.
"""
def __init__(self, img):
self.img = img
self.x = -1
self.y = -1
self.orientation = -1
def moment(p, q, img):
"""
Computes the moment (p, q) of and image.
Parameters
----------
p : int
q : int
img : 2D array
Binary image
Returns
-------
moment = float
The moment of orders (p,q) of the image img.
"""
moment = 0.
for i in range(len(img[0, :])):
for j in range(len(img[:, 0])):
moment = moment + i**p * j**q * img[j, i]
return moment
def parameter(self):
"""
Computes parameters of the object to track.
Returns
-------
x = float
The x position of the barycenter of the object.
y = float
The y position of the barycenter of the object.
orientation = float
The angle of the object, taking in account his direction.
"""
moo = ellipse.moment(0, 0, self.img)
self.x = ellipse.moment(1, 0, self.img)/moo
self.y = ellipse.moment(0, 1, self.img)/moo
i = ellipse.moment(2, 0, self.img)/moo - self.x**2
j = ellipse.moment(1, 1, self.img)/moo - self.x*self.y
k = ellipse.moment(0, 2, self.img)/moo - self.y**2
self.orientation = -0.5 * np.arctan((2*j)/(i-k)) - (i<k)*(np.pi*0.5)
self.orientation = self.orientation + 2*np.pi*(self.orientation<0)
# Computes the direction of the object to track with the skewness.
imgRot = scipy.ndimage.rotate(self.img, (-self.orientation*180)/np.pi, axes=(0, 1)) # Rotates the image to hace the major axis of the ellipse horizontal
tmp = []
for i in range(len(imgRot[0, :])): # Builds the distribution of points of the object on the major axis
tmp.append(int(np.mean(imgRot[:,i]))*[i+1])
tmp = np.concatenate(tmp, axis = 0)
skew = scipy.stats.skew(tmp)
if skew > 0:
self.orientation = (self.orientation + np.pi) % (2*np.pi)
return self.x, self.y, self.orientation
def drawArrow(self):
"""
Draws the image and an arrow to show the center of mass and an arrow to show the direction of the object.
"""
if self.x == -1 and self.y == -1 and self.orientation == -1:
[self.x, self.y, self.orientation] = self.parameter()
fig, ax = plt.subplots()
im = ax.imshow(self.img, cmap='Greys')
ax.scatter(self.x, self.y, color = 'r')
ax.arrow(self.x, self.y, 50*np.cos(-self.orientation), 50*np.sin(-self.orientation), head_width=20, head_length=20, fc='r', ec='r')
return([fig])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment