Skip to content

Instantly share code, notes, and snippets.

@macrat
Created October 31, 2016 08:32
Show Gist options
  • Save macrat/ed4d122b7d721a0deffee873412ada5f to your computer and use it in GitHub Desktop.
Save macrat/ed4d122b7d721a0deffee873412ada5f to your computer and use it in GitHub Desktop.
頭が大きくなるカメラ。LinuxClubの学祭での展示物。
import time
import cv2
import numpy
cam = cv2.VideoCapture(0)
def take_photo():
return cv2.cvtColor(cam.read()[1], cv2.COLOR_BGR2GRAY)
def make_et(img):
s = ((img.shape[1]*0.2, 0),
(img.shape[1]*0.6, 0),
(img.shape[1], img.shape[0]*0.4),
(0, img.shape[0]))
d = ((0, 0),
(img.shape[1], 0),
(img.shape[1], img.shape[0]),
(0, img.shape[0]))
return s, cv2.getPerspectiveTransform(numpy.array(s, numpy.float32),
numpy.array(d, numpy.float32))
if __name__ == '__main__':
s, m = make_et(take_photo())
s = [(int(x[0]), int(x[1])) for x in s]
while True:
img = take_photo()
cv2.namedWindow('screen', 0x10)
warp = cv2.warpPerspective(img, m, (img.shape[1], img.shape[0]))
cv2.putText(warp, 'Yo! Linux', (img.shape[1]-1200, img.shape[0]-20), cv2.FONT_HERSHEY_PLAIN, 16.0, (0, 0, 0), 8)
cv2.imshow('screen', warp)
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
for i in range(len(s)):
cv2.line(img, s[i], s[i%len(s)], (0, 255, 0), 2)
for p in s:
cv2.circle(img, p, 6, (0, 255, 0), -1)
cv2.circle(img, p, 8, (0, 255, 0), 1)
cv2.namedWindow('control', 0x10)
cv2.imshow('control', img)
k = cv2.waitKey(15)
if k == 27:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment