-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcapture_webcam.py
More file actions
executable file
·80 lines (60 loc) · 2.43 KB
/
capture_webcam.py
File metadata and controls
executable file
·80 lines (60 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/local/bin/python3
# Author: Samuel Wejeus (samuel@isalldigital.com)
import cv2
import numpy as np
import time
import os
import subprocess
import sys
# Configuration
OUTPUT_DIR = "~/Pictures/gitpic/"
DEBUG = False
def capture(cameraFeed, path, message):
fontFace = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
thickness = 1;
padding = 10
# we need couple of frames before camera has adjusted to light conditions
frame = None
for x in range(0, 8):
_, frame = cameraFeed.read()
# add commit message
(_, textHeight), _ = cv2.getTextSize(message, fontFace, fontScale, thickness);
textPosY = textHeight
lines = message.splitlines()
for line in lines:
position = (padding, textPosY+padding)
# little hack to get a outline on text since opencv does not support it natively
cv2.putText(frame, line, position, fontFace, fontScale, (0, 0, 0), thickness+2, lineType = 0)
cv2.putText(frame, line, position, fontFace, fontScale, (255, 255, 255), thickness, lineType = cv2.LINE_AA)
textPosY = textPosY + textHeight + padding
# output final image
cv2.imwrite(path, frame)
# Program ----------------------------------------------
print("gitpic -> start capture..")
projectBasePath = subprocess.Popen(["git", "rev-parse", "--show-toplevel"], stdout=subprocess.PIPE).communicate()[0].decode('ascii')
commitMessage = subprocess.Popen(["git", "log", "--format=%B", "-n", "1 HEAD"], stdout=subprocess.PIPE).communicate()[0].strip().decode('ascii')
print(commitMessage)
if DEBUG:
projectBasePath = os.path.dirname(os.path.abspath(__file__))
commitMessage = "Debug commit message\nspanning multiple\nlines\n=)"
if projectBasePath == "" or commitMessage == "":
print("Error: no project or message!")
sys.exit(1)
projectName = os.path.basename(projectBasePath).strip()
print("gitpic -> project: {0}".format(projectName))
uri = os.path.expanduser(OUTPUT_DIR + projectName + "/")
# create dir if needed
cmd_makeDirs = "mkdir -pv " + os.path.dirname(uri)
ret = subprocess.call(cmd_makeDirs, shell=True)
if ret != 0:
print("gitpitc: error: could not create needed dirs!")
sys.exit(1)
# prepare output filename
filename = time.strftime("%Y-%d-%m-%H-%M") + ".png"
imagePath = uri + filename
if DEBUG:
print("gitpic -> " + commitMessage + " -> " + imagePath)
# init opencv camera
cameraFeed = cv2.VideoCapture(0)
capture(cameraFeed, imagePath, commitMessage)