Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/opencv/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This dir contains some quick&dirty live camera examples for the 31C3 display in the entrance area.
They use OpenCV to grab webcam frames and either binarize them or detect edges and then display the result on the flipdot displays.
84 changes: 84 additions & 0 deletions scripts/opencv/edge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/python2
import numpy as np
import cv2
import socket,time,math

cap = cv2.VideoCapture(0)


sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)

FD_LEFT="2001:67c:20a1:1095:c49d:e22d:6891:dcd"
FD_MIDDLE="2001:67c:20a1:1095:34b1:6957:8ddb:3a79"
FD_RIGHT="2001:67c:20a1:1095:552a:1594:871f:d9c2"


UDPPORT=2323

def send(image,dest):
msg = '';
pieces = '';
for line in image:
pieces += ''.join(str(x) for x in line)

pieces = [pieces[i:i+8] for i in range(0, len(pieces), 8)]

for i in pieces:
if (len(i) < 8):
i = i.ljust(8, '1')
msg += chr(int(str(i), 2))

sock.sendto(msg, (dest, UDPPORT))

def partition(img):
rows,cols = img.shape

dst = cv2.transpose(img)

cv2.imshow("rot",dst)

left = dst[0:cols/3,0:rows]
middle = dst[cols/3+1:2*cols/3,0:rows]
right = dst[2*cols/3+1:cols,0:rows]

# left = img[0:img.shape[0],0:img.shape[1]/3]
# middle = img[0:img.shape[0],img.shape[1]/3+1:2*img.shape[1]/3]
# right = img[0:img.shape[0],2*img.shape[1]/3+1:img.shape[1]]
return left,middle,right




while(True):
# Capture frame-by-frame
ret, frame = cap.read()

dim=(144,120)
resized=cv2.resize(frame,dim)

resized = cv2.flip(resized,0)

# Our operations on the frame come here
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)

canny = cv2.Canny(gray,50,200)
thresh = (255-canny)/255

# Display the resulting frame
cv2.imshow('frame',gray)
cv2.imshow('canny',thresh*255)
left,middle,right = partition(thresh)
send(left,FD_LEFT)
send(middle,FD_MIDDLE)
send(right,FD_RIGHT)
cv2.imshow("left",left*255)
cv2.imshow("middle",middle*255)
cv2.imshow("right",right*255)

if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(0.7)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
117 changes: 117 additions & 0 deletions scripts/opencv/scroll_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/python2
import numpy as np
import cv2
import socket,time,math
import sys



sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)

FD_LEFT="2001:67c:20a1:1095:c49d:e22d:6891:dcd"
FD_MIDDLE="2001:67c:20a1:1095:34b1:6957:8ddb:3a79"
FD_RIGHT="2001:67c:20a1:1095:552a:1594:871f:d9c2"

UDPPORT=2323

IMG_THRESHOLD=128



def send(image,dest):
msg = '';
pieces = '';
for line in image:
pieces += ''.join(str(x) for x in line)

pieces = [pieces[i:i+8] for i in range(0, len(pieces), 8)]

for i in pieces:
if (len(i) < 8):
i = i.ljust(8, '1')
msg += chr(int(str(i), 2))

#sock.sendto(msg, (dest, UDPPORT))

def partition(img):
rows,cols = img.shape

dst = cv2.transpose(img)

cv2.imshow("rot",dst)

left = dst[0:cols/3,0:rows]
middle = dst[cols/3+1:2*cols/3,0:rows]
right = dst[2*cols/3+1:cols,0:rows]

# left = img[0:img.shape[0],0:img.shape[1]/3]
# middle = img[0:img.shape[0],img.shape[1]/3+1:2*img.shape[1]/3]
# right = img[0:img.shape[0],2*img.shape[1]/3+1:img.shape[1]]
return left,middle,right


frameSkip = 0
thresholdRadius = 75

img = cv2.imread(sys.argv[1])


r = 120 /float(img.shape[0])
dim = (int(img.shape[1] * r),120)
if dim[0] < 144:
sys.exit(1)

offset = 0

print img.shape,r, dim
fullImage = cv2.resize(img,dim)

cv2.imshow("resized",fullImage)

done = False
while not done: #offset+144 < fullImage.shape[1]:
dim=(144,120)

print fullImage.shape
if offset + 144 > fullImage.shape[1]:
offset = fullImage.shape[1] - 144
done = True
disp = fullImage[0:120,offset:offset+144]

resized = cv2.flip(disp,0)

# Our operations on the frame come here
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)

#thresh = cv2.adaptiveThreshold(gray,1,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,thresholdRadius,-2)
ret,thresh = cv2.threshold(gray,IMG_THRESHOLD,1,cv2.THRESH_BINARY)

# Display the resulting frame
cv2.imshow('frame',gray)
cv2.imshow('canny',thresh*255)
left,middle,right = partition(thresh)
send(left,FD_LEFT)
send(middle,FD_MIDDLE)
send(right,FD_RIGHT)
cv2.imshow("left",left*255)
cv2.imshow("middle",middle*255)
cv2.imshow("right",right*255)

key = cv2.waitKey(2050)

if key & 0xFF == ord(' '):
frameSkip = 30*120

if key & 0xFF == ord('w'):
thresholdRadius = thresholdRadius + 2
print "new thresh radius: ",thresholdRadius
if key & 0xFF == ord('s'):
thresholdRadius = thresholdRadius - 2
print "new thresh radius: ",thresholdRadius


if key & 0xFF == ord('q'):
break

offset += 20

9 changes: 9 additions & 0 deletions scripts/opencv/sendxkcd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

while true
do
./xkcd.py
../display foo.png

sleep 10s
done
86 changes: 86 additions & 0 deletions scripts/opencv/threshold-xkcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/python3
import numpy as np
import cv2
import socket,time,math
from xkcd import grabber

sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)

FD_LEFT="2001:67c:20a1:1095:c49d:e22d:6891:dcd"
FD_MIDDLE="2001:67c:20a1:1095:34b1:6957:8ddb:3a79"
FD_RIGHT="2001:67c:20a1:1095:552a:1594:871f:d9c2"

UDPPORT=2323

IMG_THRESHOLD=64



def send(image,dest):
msg = '';
pieces = '';
for line in image:
pieces += ''.join(str(x) for x in line)

pieces = [pieces[i:i+8] for i in range(0, len(pieces), 8)]

for i in pieces:
if (len(i) < 8):
i = i.ljust(8, '1')
msg += chr(int(str(i), 2))
if (len(i) < 8):
i = i.ljust(8, '1')

sock.sendto(msg, (dest, UDPPORT))

def partition(img):
rows,cols = img.shape

dst = cv2.transpose(img)

cv2.imshow("rot",dst)

left = dst[0:cols/3,0:rows]
middle = dst[cols/3+1:2*cols/3,0:rows]
right = dst[2*cols/3+1:cols,0:rows]

# left = img[0:img.shape[0],0:img.shape[1]/3]
# middle = img[0:img.shape[0],img.shape[1]/3+1:2*img.shape[1]/3]
# right = img[0:img.shape[0],2*img.shape[1]/3+1:img.shape[1]]
return left,middle,right




while(True):
# Capture frame-by-frame
ret, frame = cv2.imread(grabber.getImage())

dim=(144,120)
resized=cv2.resize(frame,dim)

resized = cv2.flip(resized,0)

# Our operations on the frame come here
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(gray,IMG_THRESHOLD,1,cv2.THRESH_BINARY)

# Display the resulting frame
cv2.imshow('frame',gray)
cv2.imshow('canny',thresh*255)
left,middle,right = partition(thresh)
send(left,FD_LEFT)
send(middle,FD_MIDDLE)
send(right,FD_RIGHT)
cv2.imshow("left",left*255)
cv2.imshow("middle",middle*255)
cv2.imshow("right",right*255)

if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(5)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
86 changes: 86 additions & 0 deletions scripts/opencv/threshold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/python2
import numpy as np
import cv2
import socket,time,math

cap = cv2.VideoCapture(0)


sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)

FD_LEFT="2001:67c:20a1:1095:c49d:e22d:6891:dcd"
FD_MIDDLE="2001:67c:20a1:1095:34b1:6957:8ddb:3a79"
FD_RIGHT="2001:67c:20a1:1095:552a:1594:871f:d9c2"

UDPPORT=2323

IMG_THRESHOLD=64



def send(image,dest):
msg = '';
pieces = '';
for line in image:
pieces += ''.join(str(x) for x in line)

pieces = [pieces[i:i+8] for i in range(0, len(pieces), 8)]

for i in pieces:
if (len(i) < 8):
i = i.ljust(8, '1')
msg += chr(int(str(i), 2))

sock.sendto(msg, (dest, UDPPORT))

def partition(img):
rows,cols = img.shape

dst = cv2.transpose(img)

cv2.imshow("rot",dst)

left = dst[0:cols/3,0:rows]
middle = dst[cols/3+1:2*cols/3,0:rows]
right = dst[2*cols/3+1:cols,0:rows]

# left = img[0:img.shape[0],0:img.shape[1]/3]
# middle = img[0:img.shape[0],img.shape[1]/3+1:2*img.shape[1]/3]
# right = img[0:img.shape[0],2*img.shape[1]/3+1:img.shape[1]]
return left,middle,right




while(True):
# Capture frame-by-frame
ret, frame = cap.read()

dim=(144,120)
resized=cv2.resize(frame,dim)

resized = cv2.flip(resized,0)

# Our operations on the frame come here
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(gray,IMG_THRESHOLD,1,cv2.THRESH_BINARY)

# Display the resulting frame
cv2.imshow('frame',gray)
cv2.imshow('canny',thresh*255)
left,middle,right = partition(thresh)
send(left,FD_LEFT)
send(middle,FD_MIDDLE)
send(right,FD_RIGHT)
cv2.imshow("left",left*255)
cv2.imshow("middle",middle*255)
cv2.imshow("right",right*255)

if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(0.55)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Loading