Skip to content

Commit

Permalink
Fixing issues with mp4 recording
Browse files Browse the repository at this point in the history
  • Loading branch information
beniroquai committed Feb 5, 2023
1 parent 22e4270 commit 23fb56c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
15 changes: 10 additions & 5 deletions imswitch/imblockly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ def start_server(httpd):

def run(server_class=HTTPServer, handler_class=StaticServer, port=1889):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
t = threading.Thread(target=start_server, args=(httpd,))
t.start()
#print('httpd started on port {}'.format(port))

try:
httpd = server_class(server_address, handler_class)
t = threading.Thread(target=start_server, args=(httpd,))
t.start()

print('httpd started on port {}'.format(port))
except Exception as e:
print('httpd failed to start on port {}'.format(port))
print(e)
return
run()


Expand Down
18 changes: 9 additions & 9 deletions imswitch/imcontrol/model/interfaces/tiscamera_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
class MockCameraTIS:
def __init__(self):
self.properties = {
'image_height': 800,
'image_width': 800,
'image_height': 500,
'image_width': 500,
'subarray_vpos': 0,
'subarray_hpos': 0,
'exposure_time': 0.1,
'subarray_vsize': 800,
'subarray_hsize': 800,
'SensorHeight': 1024,
'SensorWidth': 1280
'subarray_vsize': 500,
'subarray_hsize': 500,
'SensorHeight': 500,
'SensorWidth': 500
}
self.exposure = 100
self.gain = 1
Expand Down Expand Up @@ -46,11 +46,11 @@ def setBinning(self, binning):
def grabFrame(self, **kwargs):
mocktype = "random_peak"
if mocktype=="focus_lock":
img = np.zeros((500, 600))
img = np.zeros((self.SensorHeight, self.SensorWidth))
beamCenter = [int(np.random.randn() * 1 + 250), int(np.random.randn() * 30 + 300)]
img[beamCenter[0] - 10:beamCenter[0] + 10, beamCenter[1] - 10:beamCenter[1] + 10] = 1
elif mocktype=="random_peak":
imgsize = (800, 800)
imgsize = (self.SensorHeight, self.SensorWidth)
peakmax = 60
noisemean = 10
# generate image
Expand All @@ -67,7 +67,7 @@ def grabFrame(self, **kwargs):
# add Poisson noise
img = img + np.random.poisson(lam=noisemean, size=imgsize)
else:
img = np.zeros((500, 600))
img = np.zeros((self.SensorHeight, self.SensorWidth))
beamCenter = [int(np.random.randn() * 30 + 250), int(np.random.randn() * 30 + 300)]
img[beamCenter[0] - 10:beamCenter[0] + 10, beamCenter[1] - 10:beamCenter[1] + 10] = 1
img = np.random.randn(img.shape[0],img.shape[1])
Expand Down
58 changes: 28 additions & 30 deletions imswitch/imcontrol/model/managers/RecordingManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from io import BytesIO
from typing import Dict, Optional, Type

import debugpy

import h5py
import zarr
import numpy as np
Expand Down Expand Up @@ -350,7 +352,7 @@ def _record(self):

shapes = {detectorName: self.__recordingManager.detectorsManager[detectorName].shape
for detectorName in self.detectorNames}

currentFrame = {}
datasets = {}
filenames = {}
Expand Down Expand Up @@ -404,14 +406,14 @@ def _record(self):

elif self.saveFormat == SaveFormat.MP4:
# Need to initiliaze videowriter for each detector
self.__logger.debug("Initialize MP4 recorder")
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fileExtension = str(self.saveFormat.name).lower()
filePath = self.__recordingManager.getSaveFilePath(f'{self.savename}_{detectorName}.{fileExtension}')
filenames[detectorName] = filePath
datasets[detectorName] = cv2.VideoWriter(filePath, fourcc, 20.0, shapes[detectorName])
#datasets[detectorName] = cv2.VideoWriter(filePath, cv2.VideoWriter_fourcc(*'MJPG'), 10, shapes[detectorName])

self.__logger.debug(shapes[detectorName])
self.__logger.debug(filePath)

elif self.saveFormat == SaveFormat.TIFF:
fileExtension = str(self.saveFormat.name).lower()
Expand All @@ -430,6 +432,7 @@ def _record(self):
= self.__recordingManager.detectorsManager[detectorName].pixelSizeUm
datasets[detectorName].attrs['writing'] = True


self.__recordingManager.sigRecordingStarted.emit()
try:
if len(self.detectorNames) < 1:
Expand Down Expand Up @@ -483,15 +486,14 @@ def _record(self):
dataset.append(newFrames)
currentFrame[detectorName] += n
elif self.saveFormat == SaveFormat.MP4:
# Need to initiliaze videowriter for each detector
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fileExtension = str(self.saveFormat.name).lower()
filePath = self.__recordingManager.getSaveFilePath(f'{self.savename}_{detectorName}.{fileExtension}')
datasets[detectorName] = cv2.VideoWriter(filePath, fourcc, 20.0, shapes[detectorName])
#datasets[detectorName] = cv2.VideoWriter(filePath, cv2.VideoWriter_fourcc(*'MJPG'), 10, shapes[detectorName])
for iframe in range(n):
frame = newFrames[iframe,:,:]
#https://stackoverflow.com/questions/30509573/writing-an-mp4-video-using-python-opencv
frame = cv2.cvtColor(cv2.convertScaleAbs(frame), cv2.COLOR_GRAY2BGR)
self.__logger.debug(type(frame))

self.__logger.debug(shapes[detectorName])
self.__logger.debug(filePath)
datasets[detectorName].write(frame)

# Things get a bit weird if we have multiple detectors when we report
# the current frame number, since the detectors may not be synchronized.
Expand Down Expand Up @@ -531,16 +533,14 @@ def _record(self):
dataset.resize(n + it, axis=0)
dataset[it:it + n, :, :] = newFrames
elif self.saveFormat == SaveFormat.MP4:
# Need to initiliaze videowriter for each detector
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fileExtension = str(self.saveFormat.name).lower()
filePath = self.__recordingManager.getSaveFilePath(f'{self.savename}_{detectorName}.{fileExtension}')
datasets[detectorName] = cv2.VideoWriter(filePath, fourcc, 20.0, shapes[detectorName])
#datasets[detectorName] = cv2.VideoWriter(filePath, cv2.VideoWriter_fourcc(*'MJPG'), 10, shapes[detectorName])

self.__logger.debug(shapes[detectorName])
self.__logger.debug(filePath)

for iframe in range(n):
frame = newFrames[iframe,:,:]
#https://stackoverflow.com/questions/30509573/writing-an-mp4-video-using-python-opencv
frame = cv2.cvtColor(cv2.convertScaleAbs(frame), cv2.COLOR_GRAY2BGR)
self.__logger.debug(type(frame))

datasets[detectorName].write(frame)

currentFrame[detectorName] += n
self.__recordingManager.sigRecordingTimeUpdated.emit(
np.around(currentRecTime, decimals=2)
Expand Down Expand Up @@ -590,15 +590,13 @@ def _record(self):
else:
dataset.append(newFrames)
elif self.saveFormat == SaveFormat.MP4:
# Need to initiliaze videowriter for each detector
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fileExtension = str(self.saveFormat.name).lower()
filePath = self.__recordingManager.getSaveFilePath(f'{self.savename}_{detectorName}.{fileExtension}')
datasets[detectorName] = cv2.VideoWriter(filePath, fourcc, 20.0, shapes[detectorName])
#datasets[detectorName] = cv2.VideoWriter(filePath, cv2.VideoWriter_fourcc(*'MJPG'), 10, shapes[detectorName])

self.__logger.debug(shapes[detectorName])
self.__logger.debug(filePath)
for iframe in range(n):
frame = newFrames[iframe,:,:]
#https://stackoverflow.com/questions/30509573/writing-an-mp4-video-using-python-opencv
frame = cv2.cvtColor(cv2.convertScaleAbs(frame), cv2.COLOR_GRAY2BGR)

datasets[detectorName].write(frame)


currentFrame[detectorName] += n

Expand Down

0 comments on commit 23fb56c

Please sign in to comment.