forked from vlachoudis/bCNC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.py
85 lines (73 loc) · 2.6 KB
/
Camera.py
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
78
79
80
81
82
83
84
85
# -*- coding: ascii -*-
# $Id$
#
# Author: vvlachoudis@gmail.com
# Date: 24-Aug-2014
try:
import cv2 as cv
except ImportError:
cv = None
try:
from Tkinter import *
except ImportError:
from tkinter import *
try:
from PIL import Image, ImageTk
except ImportError:
cv = None
import Utils
#-------------------------------------------------------------------------------
def hasOpenCV(): return cv is not None
#===============================================================================
# Camera processing class
# A wrapper to opencv needed functions
#===============================================================================
class Camera:
#-----------------------------------------------------------------------
# prefix is the prefix to get configuration parameters from ini
#-----------------------------------------------------------------------
def __init__(self, prefix=""):
if cv is None: return
self.prefix = prefix
self.idx = Utils.getInt("Camera", prefix)
self.camera = cv.VideoCapture(self.idx)
self.image = None
self.imageTk = None
self.set()
#-----------------------------------------------------------------------
def set(self):
width = Utils.getInt("Camera", self.prefix+"_width", 0)
if width: self.camera.set(3, width)
height = Utils.getInt("Camera", self.prefix+"_height", 0)
if height: self.camera.set(4, height)
self.angle = Utils.getInt("Camera", self.prefix+"_angle")//90 % 4
#-----------------------------------------------------------------------
# Rotate image in steps of 90deg
#-----------------------------------------------------------------------
def rotate90(self, image):
if self.angle == 1: # 90 deg
return cv.transpose(image)
elif self.angle == 2: # 180 deg
return cv.flip(image,-1)
elif self.angle == 3: # 270 deg
return cv.flip(cv.transpose(image), 1)
else:
return image
#-----------------------------------------------------------------------
# Read one image and rotated if needed
#-----------------------------------------------------------------------
def read(self):
s,self.image = self.camera.read()
if s: self.image = self.rotate90(self.image)
return s,self.image
#-----------------------------------------------------------------------
# Save image to file
#-----------------------------------------------------------------------
def save(self, filename):
cv.imwrite(filename, self.image)
#-----------------------------------------------------------------------
def toTk(self):
self.imagetk = ImageTk.PhotoImage(
image=Image.fromarray(
cv.cvtColor(self.image, cv.COLOR_BGR2RGB), "RGB"))
return self.imagetk