Monday, 12 December 2016

Coding! on the Rasberry Pi

For this unit, I decided to use coding because I was determined to push and challenge myself.

I have been learning how to code on Microsoft Visual Studio and openFrameworks because I had gotten a source-code from Github. It was called video grabber and it would have connected to a Kinect camera to detect motion and create some particles of the user's silhouette using openFrameworks and video grabber. Something was going wrong with the source-code so I had to think of a brand new idea and with this idea came a new learning curve, which was the Raspberry Pi and a new coding software (Python).

First of all, I need to write down exactly what I want the code to do. I had learnt this from earlier on when I was using online tutorials to learn Visual Studio and openFrameworks.

This is basically the code in "English" not coding language.


My kind housemate Yasmin, has worked with the Rasbperry Pi before so she taught me how to properly create a circuit board on the Pi that would connect everything in order for my code to work.





I needed the following things-
  • A Raspberry Pi
  • A RasPi camera
  • GPIO pins
  • HDMI cable
  • A breadboard and some buttons
  • Keyboard
  • Mouse
  • A memory card
  • A screen
After creating the circuit board and having my set up ready, it was time to format the memory card and get a OMXPlayer installed from Github onto the Raspberry Pi because it will be the path the videos will take to play.

Yasmin kindly sat with me for the day and I learnt how to start the code in Python (which i had never used before). She also showed me how to run tests to see if everything connected to the circuit board is working well. 

After running all the applications and everything was working well. She sat with me as I tried to work through this code and helped me out with debugging etc. 

I created the dependencies which were these: 

import tweepy
from omxplayer import OMXPlayer
from time import sleep
from PIL import Image
from picamera import PiCamera
from gpiozero import Button
from subprocess import call  
from datetime import datetime  
import sys
import os

and I was ready to start building the code.


This is the test code that I ended up with and was running it through the terminal to test if it worked. There were very many bugs with getting the videos to play one after the other using OMXPlayer, the code just wouldn't run. The video would either cut off before the second video played or it just wouldn't play the second video at all. This was a huge problem because to fix this issue, I would have had to create a coded playlist of all the videos which would've taken me weeks. I didn't have that much time left to work on this project unfortunately. 

So the next solution was to have one video that would run through all the signs and give the option of a button press which would turn on the camera, pause the video at the same time then wait till the tweet is sent to continue playing the paused video.


This was the code I used for that.

Error! Not another error surely? There was a problem with the amount of time the computer was going to sleep for whilst the video was paused to open the camera. This function for some reason kept crashing the entire Pi. 

This shows another test code to have the video pause and continue playing after the camera is closed a tweet is sent to twitter.


I tried to debug it for hours but nothing I was doing was working well so it was on to plan #3. There was no way to stop this code from crashing.

This was the first code test that went through to twitter and actually live tweeted the test.


Plan #3
  1. I will have 1 long video, describing each sign, throughout the video the user can interact with it by pressing the red button. At any point throughout the video. 
  2. The code will play the video and wait for a button press (the user will be prompted throughout the video) 
  3. The video will stop, camera will open with an overlay image while the computer sleeps.
  4. The user will be prompted to press the button to send the picture to twitter.
  5. Picture will be tweeted using Tweepy and Twitter API with an overlay image, hashtag, text and timecode that I put into the code.
  6. The Pi will then delete the tweet from it's storage to save space.
  7. The video would then reloop and start again from the beginning.
  8. If no button is pressed at any point throughout the video, then the video will automatically reloop. 
After spending hours and hours trying to figure out how I was going to do this, plan 3 worked.

The one that finally works!!!!

CLICK HERE TO VIEW CODE



No comments:

Post a Comment