several steps later

This commit is contained in:
Jan Grewe 2021-01-21 08:33:02 +01:00
parent 464a3f5813
commit 768ae7f0b6
4 changed files with 154 additions and 7 deletions

View File

@ -1,6 +1,8 @@
import matplotlib.pyplot as plt
import cv2
import os
import sys
from IPython import embed
class ImageMarker:
@ -29,7 +31,7 @@ class ImageMarker:
success, frame = video.read()
frame_counter += 1
if success:
self._fig.gca().imshow(frame)
self._fig.gca().imshow(frame, origin='lower')
else:
print("Could not read frame number %i either failed to open movie or beyond maximum frame number!" % frame_number)
return []
@ -141,6 +143,9 @@ if __name__ == "__main__":
feeder_task = MarkerTask("Feeder positions", list(map(str, range(1, 2))), "Mark feeder positions")
tasks = [tank_task, feeder_task]
im = ImageMarker(tasks)
vid1 = "2020.12.11_lepto48DLC_resnet50_boldnessDec11shuffle1_200000_labeled.mp4"
marker_positions = im.mark_movie(vid1, 10000)
# vid1 = "2020.12.11_lepto48DLC_resnet50_boldnessDec11shuffle1_200000_labeled.mp4"
print(sys.argv[0])
print (sys.argv[1])
vid1 = sys.argv[1]
marker_positions = im.mark_movie(vid1, 10)
print(marker_positions)

112
setup_config.py Normal file
View File

@ -0,0 +1,112 @@
import pandas as pd
import glob
import os
import image_marker as im
import numpy as np
import tracking_result as tr
from IPython import embed
import matplotlib.pyplot as plt
# wir estellen eine Tabelle, die fuer jeden Tag die Setup Configuration enthält.
# Folgende Spalten:
# Versuchstag, Datum, light_on_area, feeder_positions, feeder_risks, temperature, salinity
x_limits = np.array([0, 1.24])
y_limits = np.array([0, 0.81])
def get_risk(position, light_area_y, x_limits, y_limits, light_risk=1):
"""Calculates the risk associated with a certain position in the arena
Args:
position (iterable): two-element vector of position i.e. (x,y)
light_area_y (iterable): two element vactor with the start and stop y-coordinates of light area
light_risk (float, optional): if position is on the bright side, a malus is added. Defaults to 1.
x_limits : extent of the tank on x axis in cm
y_limits : extent of the tank on y axis in cm
Returns:
float: the risk for this position
"""
min_wall_dist_x = min(np.abs(position[0] - x_limits))
min_wall_dist_y = min(np.abs(position[1] - y_limits))
risk_x = 1/(max(x_limits)/2) * min_wall_dist_x
risk_y = 1/(max(y_limits)/2) * min_wall_dist_y
total_risk = min(risk_x, risk_y)
is_position_on_the_bright_side = position[1] >= light_area_y[0] and position[1] < light_area_y[1]
if is_position_on_the_bright_side:
total_risk = total_risk + light_risk
return total_risk
def get_feeder_risks(feeder_positions, light_on_area):
feeder_risks = {}
if light_on_area['light_center'][1] < light_on_area['left'][1]:
light_area = [0, light_on_area['left'][1]]
else:
light_area = [light_on_area['left'][1], y_limits[1]]
for k in feeder_positions.keys():
feeder_risks[k] = get_risk(feeder_positions[k], light_area, x_limits, y_limits)
embed()
def get_feeder_positions(vid):
feeder_task = im.MarkerTask("Feeder positions", list(map(str, range(1, 9))), "Mark feeder positions")
tasks = [feeder_task]
image_marker = im.ImageMarker(tasks)
feeder_positions = image_marker.mark_movie(vid, 100)
for k in feeder_positions[0].keys():
pos = feeder_positions[0][k]
new_pos = tr.coordinate_transformation(pos)
feeder_positions[0][k] = new_pos
return feeder_positions[0]
def get_light_on_area(vid):
light_on_task = im.MarkerTask("Light ON", ["left", "right", "light_center"], "Mark light on area")
tasks = [light_on_task]
image_marker = im.ImageMarker(tasks)
marker_positions = image_marker.mark_movie(vid, 100)
for k in marker_positions[0].keys():
pos = marker_positions[0][k]
new_pos = tr.coordinate_transformation(pos)
marker_positions[0][k] = new_pos
print(marker_positions)
return marker_positions[0]
def check_day(path):
vids = sorted(glob.glob(os.path.join(path, '*.mp4')))
if len(vids) < 1:
return
vid = vids[0]
la = get_light_on_area(vid)
fp = get_feeder_positions(vid)
fr = get_feeder_risks(fp, la)
pass
if __name__ == '__main__':
folder = '/mnt/movies/merle_verena/boldness/labeled_videos'
days = sorted(glob.glob(os.path.join(folder, 'day*')))
x_range = np.arange(0.0, 1.24, .01)
y_range = np.arange(0.0, .82, .01)
risk_matrix = np.zeros((len(x_range), len(y_range)))
"""
light_on = [0.5687952439426905, 0.82]
#light_on = [0, 0.5687952439426905]
# light_on = [45.5, 81]
for i, x in enumerate(x_range):
for j, y in enumerate(y_range):
risk_matrix[i, j] = get_risk([x, y], light_on, x_limits=x_limits, y_limits=y_limits )
plt.imshow(risk_matrix.T, origin='lower')
plt.show()
exit()
"""
for d in days:
check_day(d)
print(d)
exit()

View File

@ -23,10 +23,11 @@ def show_tracking_results(dlc_results_file):
def show_image_marking(video_file):
tank_task = im.MarkerTask("tank limits", ["bottom left corner", "top left corner", "top right corner", "bottom right corner"], "Mark tank corners")
feeder_task = im.MarkerTask("Feeder positions", list(map(str, range(1, 9))), "Mark feeder positions", color="tab:red", marker="s")
tasks = [tank_task, feeder_task]
dark_light_task = im.MarkerTask('Dark side', ['left', 'right', 'dark_center'], 'Mark light dark separator line')
tasks = [tank_task, feeder_task, dark_light_task]
image_marker = im.ImageMarker(tasks)
marker_positions = image_marker.mark_movie(video_file, 1)
marker_positions = image_marker.mark_movie(video_file, 100)
for t in marker_positions:
print(t)
@ -37,7 +38,7 @@ def main(dlc_results_file, video_file):
if __name__ == "__main__":
filename = "2020.12.11_lepto48DLC_resnet50_boldnessDec11shuffle1_200000.h5"
video = "2020.12.11_lepto48DLC_resnet50_boldnessDec11shuffle1_200000_labeled.mp4"
filename = "../boldness/videos/day_8/2020.12.11_lepto48DLC_resnet50_boldnessDec11shuffle1_200000.h5"
video = "../boldness/videos/day_8/2020.12.11_lepto48DLC_resnet50_boldnessDec11shuffle1_200000_labeled.mp4"
main(filename, video)

View File

@ -4,6 +4,18 @@ import numpy as np
import numbers as nb
import os
x_0 = 116
y_0 = 156
x_factor = 1.24/1648 # Einheit m/px
y_factor = 0.81/748 # Einheit m/px
def coordinate_transformation(position):
x = (position[0] - x_0) * x_factor
y = (position[1] - y_0) * y_factor
return (x, y) #in m
class TrackingResult():
def __init__(self, results_file) -> None:
@ -40,6 +52,21 @@ class TrackingResult():
return self._positions
def position_values(self, scorer=0, bodypart=0, framerate=30):
"""returns the x and y positions in m and the likelihood of the positions.
Args:
scorer (int, optional): [description]. Defaults to 0.
bodypart (int, optional): [description]. Defaults to 0.
framerate (int, optional): [description]. Defaults to 30.
Raises:
ValueError: [description]
ValueError: [description]
Returns:
[type]: [description]
"""
if isinstance(scorer, nb.Number):
sc = self._scorer[scorer]
elif isinstance(scorer, str) and scorer in self._scorer:
@ -54,7 +81,9 @@ class TrackingResult():
raise ValueError("Bodypart %s is not in dataframe!" % bodypart)
x = self._data_frame[sc][bp]["x"] if "x" in self._positions else []
x = (np.asarray(x) - x_0) * x_factor
y = self._data_frame[sc][bp]["y"] if "y" in self._positions else []
y = (np.asarray(y) - y_0) * y_factor
l = self._data_frame[sc][bp]["likelihood"] if "likelihood" in self._positions else []
time = np.arange(len(self._data_frame))/framerate
return time, x, y, l, bp