Showing posts with label TRENDnet. Show all posts
Showing posts with label TRENDnet. Show all posts

Friday, January 13, 2012

Using Python and wxPython to Display A Motion JPEG From the TRENDnet Wireless Internet Camera

import httplib
import base64
import StringIO
import threading
import time
import wx

class Trendnet():
  
  def __init__(self, ip='1.1.1.1', username='admin', password='admin'):
    self.IP = ip
    self.Username = username
    self.Password = password
    self.Connected = False

  def Connect(self):
    if self.Connected == False:
      try:
        print 'Atempting to connect to camera', self.IP, self.Username, self.Password
        h = httplib.HTTP(self.IP)
        h.putrequest('GET','/cgi/mjpg/mjpeg.cgi')
        h.putheader('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (self.Username, self.Password))[:-1])
        h.endheaders()
        errcode, errmsg, headers = h.getreply()
        self.File = h.getfile()
        print 'Connected!'
        self.Connected = True
      except:
        print 'Unable to connect!'
        self.Connected = False
      
  def Disconnect(self):
    self.Connected = False
    print 'Camera Disconnected!'

  def Update(self):
    if self.Connected: 
      s = self.File.readline()    # '--myboundry'
      s = self.File.readline()    # 'Content-Length: #####'
      framesize = int(s[16:])
      s = self.File.read(framesize)  # jpeg data
      while s[0] != chr(0xff):
        s = s[1:]
      return StringIO.StringIO(s)
      
class CameraPanel(wx.Panel):
  
  def __init__(self, parent, camera):
    wx.Panel.__init__(self, parent, id=wx.ID_ANY, style=wx.SIMPLE_BORDER)
    self.Camera = camera
    self.Bind(wx.EVT_PAINT, self.OnPaint)
    self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
  
  def OnEraseBackground(self, event):
    pass
  
  def OnPaint(self, event):
    dc = wx.BufferedPaintDC(self)
    
    if self.Camera.Connected == True:
      try:
        stream = self.Camera.Update()
        if stream != None:
          img = wx.ImageFromStream(stream)
          bmp = wx.BitmapFromImage(img)
          dc.DrawBitmap(bmp, 0, 0, True)
      except:
        pass
    else:
      dc.SetBrush(wx.WHITE_BRUSH)
      dc.DrawRectangle(-1, -1, 620, 480)
      
    dc.DrawCircle(340, 270, 5)
    dc.DrawCircle(280, 270, 5)

if __name__ == '__main__':

  def CamThread():
    while True:
      campanel.Refresh()
      time.sleep(.01)
      
  app = wx.App(0)
  
  wx.Log_SetActiveTarget(wx.LogStderr())
  
  frame = wx.Frame(parent=None, id=wx.ID_ANY, title='UpCam.py', style=wx.DEFAULT_FRAME_STYLE & ~wx.RESIZE_BORDER)
  camera = Trendnet('192.168.1.102')
  camera.Connect()
  campanel = CameraPanel(frame, camera)
  campanel.SetSize((620,480))
  
  sizer = wx.BoxSizer(wx.VERTICAL)
  sizer.Add(campanel, 1, wx.EXPAND|wx.ALL, 5)
  frame.SetSizer(sizer)
  frame.Fit()
  frame.Show(True)
  
  thread = threading.Thread(target=CamThread)
  thread.start()
  
  app.MainLoop()

Wednesday, November 17, 2010

Gold Rush Turret Taking Shape

Work continues again on Gold Rush's mechanical aspects after a long period of working on electronics and software. My focus has been on creating a robust turret with pan and tilt capability that mounts the Trendnet IP camera and dual Matyo Toy tank guns.


The goal over the next few weeks is to continue to mount all of the required Mech Warfare gear. Once all everything is mounted I can shift focus over towards tuning the walking gaits can remote control.

Monday, May 10, 2010

Introducing IPMechCam

IPMechCam is a cross platform python program to display a motion jpeg stream from an IP camera. The intent is to provide a powerful, customizable and easy to use application for anyone to use in the Mech Warfare robotics competition.

By default IPMechCam will support the TRENDnet TV-IP100W camera with the option to expand support for other similar IP cameras.

The most interesting feature of IPMechCam will be its support for highly customizable HUD or Heads Up Display widgets. HUD widgets are able to display information in a visual manner over the top of IPMechCam's camera image. Information can include cross hairs to indicate targeting, sensor and control status relayed back from the mech and even indication of game status such as time remaining in the match and the current health of both you and your opponent.

IPMechCam is the first tool in a line of applications geared for the Mech Warfare robotic competition. Other tools planned include gait visualization and optimization, gait generation, pose/sequence editor and robot control interface.

Current progress can already be viewed in this thread of the Trossen Robotics community forums.

Friday, February 26, 2010

Using Python and Pygame to Display A Motion JPEG From the TRENDnet Wireless Internet Camera

1:  import time  
2:  import httplib  
3:  import base64  
4:  import StringIO  
5:  import pygame  
6:  from pygame.locals import *  
7:    
8:  class trendnet:  
9:    
10:      def __init__(self, ip, username='admin', password='admin'):  
11:    
12:          self.ip = ip  
13:          self.username = username  
14:          self.password = password  
15:          self.base64string = base64.encodestring('%s:%s' % (username, password))[:-1]  
16:    
17:      def connect(self):  
18:    
19:          h = httplib.HTTP(self.ip)  
20:          h.putrequest('GET','/cgi/mjpg/mjpeg.cgi')  
21:          h.putheader('Authorization', 'Basic %s' % self.base64string)  
22:          h.endheaders()  
23:          errcode, errmsg, headers = h.getreply()  
24:          self.file = h.getfile()  
25:    
26:      def update(self, window, size, offset):          
27:            
28:          data = self.file.readline()  
29:          if data[0:15] == 'Content-Length:':  
30:              count = int(data[16:])  
31:              s = self.file.read(count)      
32:              while s[0] != chr(0xff):  
33:                  s = s[1:]       
34:                    
35:              p = StringIO.StringIO(s)  
36:                
37:              try:  
38:                  campanel = pygame.image.load(p).convert()  
39:                  campanel = pygame.transform.scale(campanel, size)  
40:                  window.blit(campanel, offset)  
41:            
42:              except Exception, x:  
43:                  print x  
44:                    
45:              p.close()  
46:                
47:  if __name__ == '__main__':  
48:    
49:    pygame.init()  
50:    screen = pygame.display.set_mode((660,500), 0, 32)  
51:      
52:    pygame.display.set_caption('trendnet.py')  
53:      
54:    background = pygame.Surface((660,500))  
55:    background.fill(pygame.Color('#E8E8E8'))  
56:    screen.blit(background, (0,0))  
57:      
58:    camera = trendnet('192.168.1.103', 'admin', 'admin')  
59:    camera.connect()  
60:      
61:    while True:  
62:      
63:      camera.update(screen, (640,480), (10,10))  
64:      pygame.display.update()  
65:      
66:      for event in pygame.event.get():  
67:        if event.type == QUIT:  
68:          sys.exit(0)  
69:            
70:      time.sleep(.01)  

download code