Go to Post *be there or be triangle, circle, square - PVCpirate [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 12-02-2015, 02:06
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Patched Widget for USB Camera in SmartDashboard

Hi all-
After hearing of and experiencing the issue with streaming the USB Camera to the SmartDashboard, I investigated the issue some, and present here what I feel is an optimal solution.

The problem appears to be that the frames being sent from the camera driver are being extracted from an MJPEG stream, and thus are missing a part of the JPEG header called the Huffman Table.

As previously noted, one solution is to use the code from the Intermediate Vision template, which works because it re-encodes the JPEG before sending it to the driver station. However, this means that the RoboRIO has to decode, then encode the JPEG image for each frame the camera sends. Also, the Intermediate Vision example places the image processing in the main control loop, which can interfere with robot control functions. Ideally, we would be able to use the CameraServer class, which doesn't process the image data and runs in separate threads from the main robot loop, reducing latency for both the camera stream and robot control.

Tl;Dr: I've created a modified version of the "USB Webcam Viewer" applet that repairs the images coming from the USB camera. Download it here, and extract it in your user directory (usually C:\Users\<username>), then add the "USB Webcam Viewer (Compat)" in SmartDashboard. More instructions here.

You can download the source here.

It reinserts the Huffman Table at the correct place in the image data before decoding the JPEG for display. This lets the driver station computer (which is likely much faster than the RoboRIO) do the work of repairing the image data, and does it much more efficiently than having to re-encode the entire image.

A slightly more efficient solution might be to use a JPEG decoder that's agnostic to the Huffman Table being missing, but the Java JPEG decoder is so entwined with the AWT framework that replacing it in a manner that wouldn't lead to more inefficiencies would be extremely difficult. In addition, the presented solution requires only a handful of memory reads and two block memory copies, so it should be very efficient.

Thanks to @TheChewanater on Reddit for posting the original code to the USB Camera Viewer widget. The algorithm to fix the JPEG header was adapted from here.
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
  #2   Spotlight this post!  
Unread 14-02-2015, 09:40
Javaru's Avatar
Javaru Javaru is offline
Registered User
AKA: Mark
FRC #3838 (RocCity Robotics)
Team Role: Mentor
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Rochester, NY
Posts: 6
Javaru is an unknown quantity at this point
Re: Patched Widget for USB Camera in SmartDashboard

Ryan,

Many, many thanks for your post. Your fix is working like a charm. You saved us a lot of headaches during crunch weekend.

Regards,
Mark
  #3   Spotlight this post!  
Unread 14-02-2015, 11:40
eddie12390's Avatar
eddie12390 eddie12390 is offline
Registered User
AKA: Eddie
FRC #3260 (SHARP)
Team Role: Programmer
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Pittsburgh
Posts: 285
eddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of light
Re: Patched Widget for USB Camera in SmartDashboard

Does there need to be anything in the code that is running on the robot? The solution from the Intermediate Vision example is working correctly but when using the new widget I just get red box stating "Error: Connection refused: Connect".
__________________
Any post that I make expresses only my opinions. I do not represent the opinions of my team or of any entity affiliated with my team.

As a Mentor:
2016 - Engineering Inspiration at Greater Pittsburgh Regional

As a Driver:
2014 - Winner of Greater Pittsburgh Regional
  #4   Spotlight this post!  
Unread 14-02-2015, 15:35
KevinWang KevinWang is offline
Registered User
FRC #5428
 
Join Date: Nov 2014
Location: Markham, Ontario
Posts: 1
KevinWang is an unknown quantity at this point
Re: Patched Widget for USB Camera in SmartDashboard

Thankyou so much, works really well.
  #5   Spotlight this post!  
Unread 15-02-2015, 00:16
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: Patched Widget for USB Camera in SmartDashboard

Quote:
Originally Posted by eddie12390 View Post
Does there need to be anything in the code that is running on the robot? The solution from the Intermediate Vision example is working correctly but when using the new widget I just get red box stating "Error: Connection refused: Connect".
Hi Eddie,
The only thing that needs to be running is the Camera Server, the code in Simple Vision is what it was intended to work with.

I actually haven't tried running with Intermediate Vision code, but I didn't change anything about the networking code, so I'm not sure what the issue might be. If you're running Intermediate Vision or otherwise post-processing the image before sending it to the drive station, there's no benefit to running my version of the applet, though, since the robot would already be fixing the image as described in my OP. If you're taking this approach, I would recommend the vanilla USB Webcam Viewer.

Best,
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
  #6   Spotlight this post!  
Unread 15-02-2015, 11:05
eddie12390's Avatar
eddie12390 eddie12390 is offline
Registered User
AKA: Eddie
FRC #3260 (SHARP)
Team Role: Programmer
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Pittsburgh
Posts: 285
eddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of light
Re: Patched Widget for USB Camera in SmartDashboard

Quote:
Originally Posted by RyanCahoon View Post
Hi Eddie,
The only thing that needs to be running is the Camera Server, the code in Simple Vision is what it was intended to work with.

I actually haven't tried running with Intermediate Vision code, but I didn't change anything about the networking code, so I'm not sure what the issue might be. If you're running Intermediate Vision or otherwise post-processing the image before sending it to the drive station, there's no benefit to running my version of the applet, though, since the robot would already be fixing the image as described in my OP. If you're taking this approach, I would recommend the vanilla USB Webcam Viewer.

Best,
I wasn't using the Intermediate Vision code, I had left out starting the automatic capture on the CameraServer.
__________________
Any post that I make expresses only my opinions. I do not represent the opinions of my team or of any entity affiliated with my team.

As a Mentor:
2016 - Engineering Inspiration at Greater Pittsburgh Regional

As a Driver:
2014 - Winner of Greater Pittsburgh Regional
  #7   Spotlight this post!  
Unread 19-02-2015, 14:20
iLloyd iLloyd is offline
Registered User
AKA: Isiah L.
FRC #0128 (The Botcats)
Team Role: Programmer
 
Join Date: Feb 2013
Rookie Year: 2013
Location: Grandview
Posts: 27
iLloyd is an unknown quantity at this point
Re: Patched Widget for USB Camera in SmartDashboard

We are using the Microsoft LifeCam and your patched widget. Sometimes, the camera feed will appear and sometimes it won't. It's not very consistent, but the light is always on the camera. We are using the Simple Vision sample in our C++ code. It would be awesome if you could help us so the camera feed is consistent.
  #8   Spotlight this post!  
Unread 19-02-2015, 16:01
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: Patched Widget for USB Camera in SmartDashboard

Quote:
Originally Posted by iLloyd View Post
Sometimes, the camera feed will appear and sometimes it won't.
When the image doesn't show, does a red box appear at the bottom of the widget with an error? Also, if my part of the code encountered an error, there should be files placed at C:\Users\<user name>\SmartDashboard\extensions\ named error.txt and frame.jpg. Can you upload them?
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
  #9   Spotlight this post!  
Unread 12-03-2015, 14:38
ThomasClark's Avatar
ThomasClark ThomasClark is offline
Registered User
FRC #0237
 
Join Date: Dec 2012
Location: Watertown, CT
Posts: 146
ThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud of
Re: Patched Widget for USB Camera in SmartDashboard

Quote:
Originally Posted by RyanCahoon View Post
Thanks to @TheChewanater on Reddit for posting the original code to the USB Camera Viewer widget. The algorithm to fix the JPEG header was adapted from here.
"TheChewanater on Reddit" here. Do you mind if we adapt this for the official SmartDashboard distribution? We've been trying to solve this exact problem for a while now, and it looks like you've got it down.
  #10   Spotlight this post!  
Unread 12-03-2015, 15:31
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: Patched Widget for USB Camera in SmartDashboard

Quote:
Originally Posted by ThomasClark View Post
Do you mind if we adapt this for the official SmartDashboard distribution?
Be my guest. That would be great.
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
  #11   Spotlight this post!  
Unread 18-03-2015, 09:45
SndMndBdy SndMndBdy is offline
Registered User
FRC #3419
 
Join Date: Jan 2013
Location: New York
Posts: 18
SndMndBdy is on a distinguished road
Re: Patched Widget for USB Camera in SmartDashboard

What would have gone wrong if you weren't using this patch? We were using a Logitech USB Camera, and the the smart dashboard extension generally worked fine but occasionally didn't show anything at all. Might the problem with the missing Huffman Table have caused that symptom?
  #12   Spotlight this post!  
Unread 18-03-2015, 11:40
eddie12390's Avatar
eddie12390 eddie12390 is offline
Registered User
AKA: Eddie
FRC #3260 (SHARP)
Team Role: Programmer
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Pittsburgh
Posts: 285
eddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of lighteddie12390 is a glorious beacon of light
Re: Patched Widget for USB Camera in SmartDashboard

Quote:
Originally Posted by SndMndBdy View Post
What would have gone wrong if you weren't using this patch? We were using a Logitech USB Camera, and the the smart dashboard extension generally worked fine but occasionally didn't show anything at all. Might the problem with the missing Huffman Table have caused that symptom?
It would have never worked correctly and the widget in the SmartDashboard would have a red bar with a message stating that the frame was missing a Huffman Table.
__________________
Any post that I make expresses only my opinions. I do not represent the opinions of my team or of any entity affiliated with my team.

As a Mentor:
2016 - Engineering Inspiration at Greater Pittsburgh Regional

As a Driver:
2014 - Winner of Greater Pittsburgh Regional
  #13   Spotlight this post!  
Unread 21-03-2015, 17:07
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,570
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Patched Widget for USB Camera in SmartDashboard

I found that the resolution property was being saved to the xml file, but on startup, the camera was always set to 640x480. My patch for that is here: https://usfirst.collab.net/gerrit/#/c/844/
  #14   Spotlight this post!  
Unread 22-03-2015, 20:05
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: Patched Widget for USB Camera in SmartDashboard

Quote:
Originally Posted by Joe Ross View Post
I found that the resolution property was being saved to the xml file, but on startup, the camera was always set to 640x480. My patch for that is here: https://usfirst.collab.net/gerrit/#/c/844/
Thanks, Joe
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 01:03.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi