Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__pycache__/
.ipynb_checkpoints/
*test*
*.pyc
write_camera_details.py
camera_details.json
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ This software package was written by [Gary Kane](https://github.com/gkane26), po
1. Install the latest driver for your camera. You can find the driver from The Imaging Source website: https://www.theimagingsource.com/products/. **If you add new cameras to an existing system, make sure to update the driver!**

1. Clone this repository. Open command prompt (type "cmd" into the search bar and hit enter), then type:<br/><br/>
``git clone https://github.com/AdaptiveMotorControlLab/camera_control``
``git clone https://github.com/AdaptiveMotorControlLab/Camera_Control``

2. Open the camera control directory, **right-click 'install1.bat' and select 'Run as administrator'**<br/><br/>
This script will install imaging source libraries, ffmpeg for command prompt, and create a new conda environment 'camera27'. Upon completion, the window will close suddenly.
This script will install imaging source libraries, ffmpeg for command prompt, and create a new conda environment 'camera36'. Upon completion, the window will close suddenly.

3. Run 'install2.bat'.<br/><br/>
This script will finish setting up the conda environment and create a desktop shortcut to open the GUI.
Expand All @@ -35,15 +35,15 @@ This script will finish setting up the conda environment and create a desktop sh
- name = camera name
- crop = cropping parameters
- rotate = rotation angle of image
- exposure = integer exposure values; can edit this in the GUI
- exposure = exposure in ms; can edit this in the GUI
- output_dir = default directory to save videos; can edit this in the GUI
- Example:


cam_0 = {'name' : 'Cog Rig',
'crop' : {'top' : 150, 'left' : 225, 'height' : 250, 'width' : 300},
'rotate' : 0,
'exposure' : -14,
'exposure' : .003,
'output_dir' : 'C:/Users/user1/Desktop/video'}


Expand Down
16 changes: 9 additions & 7 deletions camera_control_GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
GUI to record from imaging source cameras during experiments
"""

from Tkinter import Entry, Label, Button, StringVar, IntVar, Tk, END, Radiobutton
import tkFileDialog
import ttk
from tkinter import Entry, Label, Button, StringVar, IntVar, Tk, END, Radiobutton, filedialog, ttk
import numpy as np
import datetime
import os
Expand All @@ -21,7 +19,6 @@
import threading
import json
import nidaqmx
import write_camera_details

class CamGUI(object):

Expand All @@ -43,8 +40,9 @@ def __init__(self):
self.selectCams()

def browse_output(self):
filepath = tkFileDialog.askdirectory()
self.output.set(filepath)
if len(self.vid_out) == 0:
filepath = filedialog.askdirectory()
self.output.set(filepath)

def init_cam(self, num):
# create pop up window during setup
Expand Down Expand Up @@ -92,7 +90,7 @@ def set_exposure(self, num):
cam_check_window.mainloop()
cam_check_window.destroy()
else:
self.cam[num].set_exposure(int(self.exposure[num].get()))
self.cam[num].set_exposure(float(self.exposure[num].get()))

def lv_interrupt(self, task_handle, signal_type, callback_data):

Expand Down Expand Up @@ -247,6 +245,7 @@ def start_record(self):
self.vid_start_time = time.time()
t = []
for i in range(len(self.cam)):
self.cam[i].set_frame_rate(int(self.fps.get()))
t.append(threading.Thread(target=self.record_on_thread, args=(i,)))
t[-1].daemon = True
t[-1].start()
Expand Down Expand Up @@ -320,6 +319,9 @@ def save_vid(self, compress=False, delete=False):

def close_window(self):

if self.record_on.get():
return

self.end_labview()
if not self.setup:
self.done = True
Expand Down
19 changes: 0 additions & 19 deletions camera_env.yaml

This file was deleted.

20 changes: 20 additions & 0 deletions camera_env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: camera36
channels:
- defaults
dependencies:
- certifi=2019.9.11=py36_0
- pip=19.2.3=py36_0
- python=3.6.9=h5500b2f_0
- setuptools=41.2.0=py36_0
- sqlite=3.29.0=he774522_0
- vc=14.1=h0510ff6_4
- vs2015_runtime=14.16.27012=hf0eaf9b_0
- wheel=0.33.6=py36_0
- wincertstore=0.2=py36h7fe50ca_0
- pip:
- ffmpy==0.2.2
- imutils==0.5.3
- nidaqmx==0.5.7
- numpy==1.16.0
- opencv-python==4.1.1.26
- six==1.12.0
75 changes: 33 additions & 42 deletions ic_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
camera class for imaging source cameras - helps load correct settings
"""

from pyicic.IC_ImagingControl import *
import tisgrabber as ic
import numpy as np
import os
import json
import cv2
import ctypes as C

path = os.path.dirname(os.path.realpath(__file__))
dets_file = os.path.normpath(path + '/camera_details.json')
with open(dets_file) as f:
cam_details = json.load(f)
cam_details = json.load(open(dets_file, 'r'))

class ICCam(object):

def __init__(self, cam_num=0, rotate=None, crop=None, exposure=None, format='Y800 (720x540)'):
def __init__(self, cam_num=0, rotate=None, crop=None, exposure=None):
'''
Params
------
Expand All @@ -31,65 +31,56 @@ def __init__(self, cam_num=0, rotate=None, crop=None, exposure=None, format='Y80
default = None, uses default parameters specific to camera
'''

self.ic_ic = IC_ImagingControl()
self.ic_ic.init_library()

self.cam_num = cam_num
self.rotate = rotate if rotate is not None else cam_details[str(self.cam_num)]['rotate']
self.crop = crop if crop is not None else cam_details[str(self.cam_num)]['crop']
self.exposure = exposure if exposure is not None else cam_details[str(self.cam_num)]['exposure']

self.cam = self.ic_ic.get_device(self.ic_ic.get_unique_device_names()[self.cam_num])
self.cam.open()
self.cam.set_video_format(format)
self.cam = ic.TIS_CAM()
self.cam.open(self.cam.GetDevices()[cam_num].decode())
self.add_filters()

def add_filters(self):
if self.rotate != 0:
h_r = self.cam.create_frame_filter('Rotate Flip')
self.cam.add_frame_filter_to_device(h_r)
self.cam.frame_filter_set_parameter(h_r, 'Rotation Angle', self.rotate)

h_c = self.cam.create_frame_filter('ROI')
self.cam.add_frame_filter_to_device(h_c)
self.cam.frame_filter_set_parameter(h_c, 'Top', self.crop['top'])
self.cam.frame_filter_set_parameter(h_c, 'Left', self.crop['left'])
self.cam.frame_filter_set_parameter(h_c, 'Height', self.crop['height'])
self.cam.frame_filter_set_parameter(h_c, 'Width', self.crop['width'])
h_r = self.cam.CreateFrameFilter(b'Rotate Flip')
self.cam.AddFrameFilter(h_r)
self.cam.FilterSetParameter(h_r, b'Rotation Angle', self.rotate)

h_c = self.cam.CreateFrameFilter(b'ROI')
self.cam.AddFrameFilter(h_c)
self.cam.FilterSetParameter(h_c, b'Top', self.crop['top'])
self.cam.FilterSetParameter(h_c, b'Left', self.crop['left'])
self.cam.FilterSetParameter(h_c, b'Height', self.crop['height'])
self.cam.FilterSetParameter(h_c, b'Width', self.crop['width'])
self.size = (self.crop['width'], self.crop['height'])

self.cam.gain.auto = False
self.cam.exposure.auto = False
self.cam.exposure.value = self.exposure

def set_frame_rate(self, fps):
self.cam.SetFrameRate(fps)

def set_exposure(self, val):
try:
val = int(round(val))
val = val if val < self.cam.exposure.max-1 else self.cam.exposure.max-1
val = val if val > self.cam.exposure.min else self.cam.exposure.min
self.cam.exposure.value = val
except:
pass
val = 1 if val > 1 else val
val = 0 if val < 0 else val
self.cam.SetPropertyAbsoluteValue("Exposure", "Value", val)

def get_exposure(self):
return self.cam.exposure.value
exposure = [0]
self.cam.GetPropertyAbsoluteValue("Exposure", "Value", exposure)
return round(exposure[0], 3)

def get_image(self):
data, width, height, depth = self.cam.get_image_data()
frame = np.ndarray(buffer=data,
dtype=np.uint8,
shape=(height, width, depth))
self.cam.SnapImage()
frame = self.cam.GetImageEx()
return cv2.flip(frame,0)

def get_image_dimensions(self):
_, width, height, _ = self.cam.get_image_data()
im = self.get_image()
height = im.shape[0]
width = im.shape[1]
return (width, height)

def start(self, show_display=True):
self.cam.enable_continuous_mode(True)
self.cam.start_live(show_display=show_display)
def start(self, show_display=1):
self.cam.SetContinuousMode(0)
self.cam.StartLive(show_display)

def close(self):
self.cam.stop_live()
self.cam.close()
self.cam.StopLive()
4 changes: 2 additions & 2 deletions install1.bat
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ powershell [System.Environment]::SetEnvironmentVariable('PATH', '%NEW_PATH%', 'M

echo.
echo.
echo Create New Conda Environment: camera27
echo Create New Conda Environment: camera36
call activate.bat
conda env create -f camera_env.yaml
conda env create -f camera_env.yml
9 changes: 2 additions & 7 deletions install2.bat
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
echo OFF

@setlocal enableextensions
@cd /d "%~dp0"

echo.
echo.
echo Install Imaging Source Python Package
call activate camera27
pip install git+https://github.com/morefigs/py-ic-imaging-control
echo Initialize system setup via write_camera_details.py file
copy write_camera_details_TEMPLATE.py write_camera_details.py
python write_camera_details.py

Expand All @@ -16,7 +11,7 @@ echo.
echo.
echo Write Shortcut File to the Desktop
echo call activate.bat > C:%HOMEPATH%\Desktop\cameraGUI.bat
echo call activate camera27 >> C:%HOMEPATH%\Desktop\cameraGUI.bat
echo call activate camera36 >> C:%HOMEPATH%\Desktop\cameraGUI.bat
echo cd %cd% >> C:%HOMEPATH%\Desktop\cameraGUI.bat
echo python camera_control_GUI.py >> C:%HOMEPATH%\Desktop\cameraGUI.bat

Expand Down
Loading