Maker unites with Biohacker — guide to live streaming of microscopic images of DIY cell culture
If you are on a DIY cell culture project, you may get an irresistible urge to share your project with fellow biohackers. Sharing is also important from a reproducibility and serendipity point of view. The latter — serendipity — more eyes to observe means higher chance of us noticing something interesting.
If something goes wrong during the experiment, streaming it online may allow for a quicker response.
This is part of a series to do that. In this first piece describes our attempt to set up a server that makes and shares time-lapse videos of slow-moving phenomenon like cell proliferation and seeds germinating.
This piece is translated from two articles in biohacker.jp with the permission of the author “fetuin”, a long-time blogger.
Original articles in Japanese:
https://biohacker.jp/c/BH296.html
https://biohacker.jp/c/BH297.html
from the series
https://biohacker.jp/incube.html
======================================
2020.11.17
Live-streaming of DIY cell culture by a webcam controlled by a Raspberry Pi
If you are on a DIY cell culture experiment, there are a million things that could go wrong while you are away for school, work, and such. It would help if you could see the cells real time online, despite the potential side effect of intermittent distractions from work or class to watch how your cells are doing….
Live-streaming of germinating radish seeds by a Raspberry Pi enabled webcam — which ultimately failed because my room was too cold.
Step1: Take photo by a webcam controlled by a Raspberry Pi
#Installs the webcam driver
sudo apt-get install fswebcam#Saves the images taken
fswebcam -r 1280x720 capture.jpg
Step2: Install a module called “requests” to send and stack images on a web server
#Installs pip required for installation
sudo apt-get install python-dev
sudo apt-get install python-pip#Installs requests by this pip
pip install requests
Step 3: Python script to stack captured images
import requests
import subprocesssubprocess.call([‘fswebcam’,’-r’,’1280x720',’capture.jpg’])
url=’http://xxxxx.com/upload.cgi'
file = {‘upload_file’: open(‘capture.jpg’,’rb’)}
data = {‘temp1’: ‘dummydata’} #if sending parameters together
r = requests.post(url, files=file, data=data)
print r.text
If you run the script over crontab, the webcam captures images at a certain time interval. The whole budget would be around $40 including Raspberry Pi and webcam.
Step 4: Perl script on the server that receives images
#! /usr/bin/perl
use CGI;
my $q = new CGI;
my $fh = $q->param(‘upload_file’);
@FORMname= $q->param();
foreach my $dammy (@FORMname){
$FORM{$dammy}=$q->param($dammy);
}print “Content-Type:text/html\n\n”;
print “OK”;$tt=time; #Transmitted files are saved by timestamp-based names.
if ($fh ne “”) {
open(F,”>”.$tt.’.jpg’) or die;
flock(F, 2);
binmode F;
if (defined $fh) {while (<$fh>) {print F $_;}}
close F;
}
exit;
I wanted to assemble .jpg files into a timelapse video, but my cloud server is not letting me install video encoders like mencoder or ffmpeg….
2020.11.26
Live-streaming of DIY cell culture ~ encoding captured and uploaded images into a time-lapse video
I have described how to stack images captured at a certain time interval using a Raspberry Pi and webcam. The next step encodes the images into a public time-lapse video.
Here is how it came about:
(video available at the source page https://biohacker.jp/c/BH297.html )
This time-lapse video covers one week. The video is updated 4 times a day. The water is topped up every night as it evaporates. A USB flashlight illuminates the setup during the night hours.
Just a disclaimer — there was a day when my Raspberry Pi was dead :-( Set up a watch dog, maybe? My room was cold and the seeds are taking a week to germinate. Bring it on, my radish! The quality of the $20 webcam was fair. It doesn’t quite focus, especially when the target is close by.
The original intention was to encode time lapse images on a server, but it didn’t let me install video encoders. Even if I could, video encoders would put a heavy load on the server, ending up with me getting an angry email from the server administrator.
So, that put me to download images from the server, encode on my edge, and re-upload the time-lapse video file. There was an unused Raspberry Pi lying around in my room.
The uploaded images and video are saved at https://kamotown.net/upload/
from ftplib import FTP
import subprocess
import os#FTP server configuration
ftp= FTP(“xxxxx.xxxx.xxx”, “yyyyyy”, passwd=”zzzzzzz”)
items=ftp.nlst(“www/kamotown/upload/”)#Downloads images not on my end from a certain directory in the server
for file in items:
if os.path.splitext(os.path.basename(file))[1] == ‘.jpg’:
if os.path.isfile(os.path.basename(file))==False:
with open(os.path.basename(file),”wb”) as f:
ftp.retrbinary(“RETR “+str(file),f.write)
print str(file)
f.close()#Saves the list of images as list.txt
subprocess.call(‘ls *.jpg>list.txt’,shell=True)
#Re-formats images to a ffmpeg compatible names and saves at list2.txt
f2=open(‘list.txt’)
f3=open(‘list2.txt’,’w’)
data=f2.readline()
while data:
f3.write(‘file ‘+data)
data=f2.readline()
f2.close()
f3.close()#Converts images by a ffmpeg subprocess
subprocess.call(‘ffmpeg -f image2 -r 15 -f concat -i list2.txt -r 15 -an -vcodec libx264 -pix_fmt yuv420p timelapse.mp4’, shell=True)
#Returns video to the server
with open(“timelapse.mp4”, “rb”) as f:
ftp.storbinary(“STOR www/kamotown/upload/timelapse.mp4”, f)
f.close()
This simple script installs ffmpeg to a Raspberry Pi :
sudo apt-get install ffmpeg
Periodic execution of this Python script by crontab or such keeps the time-lapse video on the server up to date. I’ve used (the cheapest) Raspberry Pi zero for encoding. It took like an hour to encode a 10MB video from 400 images….
To set up a public timelapse video sharing service, I need to think about the encoder. Oh, perhaps, setting “-vcodec h264_omx” for ffmpeg encoder activates hardware encoding?