oskarastrom commited on
Commit
d241eff
·
1 Parent(s): 64beaa7

Didson check

Browse files
Files changed (2) hide show
  1. app.py +0 -2
  2. backend/aris.py +56 -42
app.py CHANGED
@@ -125,8 +125,6 @@ def infer_next(_, progress=gr.Progress()):
125
 
126
  #crop_clip(file_path, 65)
127
 
128
- aris_info, frame = pyARIS.DataImport(file_path)
129
-
130
  # Do inference
131
  json_result, json_filepath, zip_filepath, video_filepath, marking_filepath = predict_task(
132
  file_path,
 
125
 
126
  #crop_clip(file_path, 65)
127
 
 
 
128
  # Do inference
129
  json_result, json_filepath, zip_filepath, video_filepath, marking_filepath = predict_task(
130
  file_path,
backend/aris.py CHANGED
@@ -265,37 +265,43 @@ def add_metadata_to_result(aris_fp, json_data, beam_width_dir=BEAM_WIDTH_DIR):
265
 
266
  metadata = {}
267
  metadata["FILE_NAME"] = aris_fp
268
- ARISdata, frame = pyARIS.DataImport(aris_fp)
269
- metadata["FRAME_RATE"] = frame.framerate
 
 
 
270
 
271
- # Load in the beam width information
272
- beam_width_data, camera_type = pyARIS.load_beam_width_data(frame, beam_width_dir=beam_width_dir)
273
 
274
- # What is the meter resolution of the smallest sample?
275
- min_pixel_size = pyARIS.get_minimum_pixel_meter_size(frame, beam_width_data)
 
276
 
277
- # What is the meter resolution of the sample length?
278
- sample_length = frame.sampleperiod * 0.000001 * frame.soundspeed / 2
279
 
280
- # Choose the size of a pixel
281
- pixel_meter_size = max(min_pixel_size, sample_length)
282
 
283
- # Determine the image dimensions
284
- xdim, ydim, x_meter_start, y_meter_start, x_meter_stop, y_meter_stop = pyARIS.compute_image_bounds(
285
- pixel_meter_size, frame, beam_width_data,
286
- additional_pixel_padding_x=0,
287
- additional_pixel_padding_y=0
288
- )
289
 
290
- # Compute the mapping from the samples to the image
291
- sample_read_rows, sample_read_cols, image_write_rows, image_write_cols = pyARIS.compute_mapping_from_sample_to_image(
292
- pixel_meter_size,
293
- xdim, ydim, x_meter_start, y_meter_start,
294
- frame, beam_width_data
295
- )
 
 
 
 
 
 
 
296
 
297
- marking_mapping = dict(zip(zip(image_write_rows, image_write_cols),
298
- zip(sample_read_rows, sample_read_cols)))
299
 
300
  # Manual marking format rounds 0.5 to 1 instead of 0 in IEEE 754
301
  def round(number, ndigits=0):
@@ -308,7 +314,7 @@ def add_metadata_to_result(aris_fp, json_data, beam_width_dir=BEAM_WIDTH_DIR):
308
  metadata["NONDIRECTIONAL_FISH"] = none # TODO
309
  metadata["TOTAL_FISH"] = metadata["UPSTREAM_FISH"] + metadata["DOWNSTREAM_FISH"] + metadata["NONDIRECTIONAL_FISH"]
310
 
311
- metadata["TOTAL_FRAMES"] = ARISdata.FrameCount
312
  metadata["EXPECTED_FRAMES"] = -1 # What is this?
313
  metadata["TOTAL_TIME"] = str(datetime.timedelta(seconds=round(metadata["TOTAL_FRAMES"]/metadata["FRAME_RATE"])))
314
  metadata["EXPECTED_TIME"] = str(datetime.timedelta(seconds=round(metadata["EXPECTED_FRAMES"]/metadata["FRAME_RATE"])))
@@ -317,11 +323,11 @@ def add_metadata_to_result(aris_fp, json_data, beam_width_dir=BEAM_WIDTH_DIR):
317
 
318
  metadata["COUNT_FILE_NAME"] = 'N/A'
319
  metadata["EDITOR_ID"] = 'N/A'
320
- metadata["INTENSITY"] = f'{round(frame.intensity, 1):.1f} dB' # Missing
321
- metadata["THRESHOLD"] = f'{round(frame.threshold, 1):.1f} dB' # Missing
322
- metadata["WINDOW_START"] = round(frame.windowstart, 2)
323
- metadata["WINDOW_END"] = round(frame.windowstart + frame.windowlength, 2)
324
- metadata["WATER_TEMP"] = f'{int(round(frame.watertemp))} degC'
325
 
326
  s = f''''''
327
 
@@ -375,7 +381,10 @@ def add_metadata_to_result(aris_fp, json_data, beam_width_dir=BEAM_WIDTH_DIR):
375
  metadata["FISH"] = []
376
  for entry in sorted(entries, key=lambda x: x['fish_id']):
377
  frame_num = entry['frame_num']
378
- frame = pyARIS.FrameRead(ARISdata, frame_num)
 
 
 
379
 
380
  y = (entry['bbox'][1]+entry['bbox'][3])/2
381
  x = (entry['bbox'][0]+entry['bbox'][2])/2
@@ -397,22 +406,22 @@ def add_metadata_to_result(aris_fp, json_data, beam_width_dir=BEAM_WIDTH_DIR):
397
  fish_entry['NBR_FRAMES'] = entry['end_frame_index'] + 1 - entry['start_frame_index']
398
  fish_entry['TRAVEL'] = entry['travel_dist']
399
  fish_entry['DIR'] = upstream_motion_map[entry['direction']]
400
- fish_entry['R'] = bin_num * pixel_meter_size + frame.windowstart
401
  fish_entry['THETA'] = beam_width_data['beam_center'][beam_num]
402
  fish_entry['L'] = entry['length']
403
  fish_entry['DR'] = -1.0 # What is this?
404
  fish_entry['LDR'] = -1.0 # What is this?
405
  fish_entry['ASPECT'] = -1.0 # What is this?
406
- TIME, DATE = datetime.datetime.fromtimestamp(frame.sonartimestamp/1000000, pytz.timezone('UTC')).strftime('%H:%M:%S %Y-%m-%d').split()
407
  fish_entry['TIME'] = TIME
408
  fish_entry['DATE'] = DATE
409
- fish_entry['LATITUDE'] = frame.latitude or 'N 00 d 0.00000 m'
410
- fish_entry['LONGITUDE'] = frame.longitude or 'E 000 d 0.00000 m'
411
- fish_entry['PAN'] = frame.sonarpan
412
  if math.isnan(fish_entry['PAN']): fish_entry['PAN'] = "nan"
413
- fish_entry['TILT'] = frame.sonartilt
414
  if math.isnan(fish_entry['TILT']): fish_entry['TILT'] = "nan"
415
- fish_entry['ROLL'] = frame.roll # May be wrong number but sonarroll was NaN
416
  fish_entry['SPECIES'] = 'Unknown'
417
  fish_entry['MOTION'] = 'Running <-->'
418
  fish_entry['Q'] = -1 #5 # I don't know what this is or where it comes from
@@ -423,10 +432,15 @@ def add_metadata_to_result(aris_fp, json_data, beam_width_dir=BEAM_WIDTH_DIR):
423
 
424
  # What are these?
425
  # Maybe the date and time range for the recording?
426
- first_frame = pyARIS.FrameRead(ARISdata, 0)
427
- last_frame = pyARIS.FrameRead(ARISdata, metadata["TOTAL_FRAMES"]-1)
428
- start_time, start_date = datetime.datetime.fromtimestamp(first_frame.sonartimestamp/1000000, pytz.timezone('UTC')).strftime('%H:%M:%S %Y-%m-%d').split()
429
- end_time, end_date = datetime.datetime.fromtimestamp(last_frame.sonartimestamp/1000000, pytz.timezone('UTC')).strftime('%H:%M:%S %Y-%m-%d').split()
 
 
 
 
 
430
  metadata["DATE"] = start_date
431
  metadata["START"] = start_time
432
  metadata["END"] = end_time
 
265
 
266
  metadata = {}
267
  metadata["FILE_NAME"] = aris_fp
268
+ if (aris_fp.endswith(".aris")):
269
+ ARISdata, frame = pyARIS.DataImport(aris_fp)
270
+ else:
271
+ ARISdata = None
272
+ frame = None
273
 
274
+ metadata["FRAME_RATE"] = frame.framerate if frame is not None else "-"
 
275
 
276
+ if (frame is not None):
277
+ # Load in the beam width information
278
+ beam_width_data, camera_type = pyARIS.load_beam_width_data(frame, beam_width_dir=beam_width_dir)
279
 
280
+ # What is the meter resolution of the smallest sample?
281
+ min_pixel_size = pyARIS.get_minimum_pixel_meter_size(frame, beam_width_data)
282
 
283
+ # What is the meter resolution of the sample length?
284
+ sample_length = frame.sampleperiod * 0.000001 * frame.soundspeed / 2
285
 
286
+ # Choose the size of a pixel
287
+ pixel_meter_size = max(min_pixel_size, sample_length)
 
 
 
 
288
 
289
+ # Determine the image dimensions
290
+ xdim, ydim, x_meter_start, y_meter_start, x_meter_stop, y_meter_stop = pyARIS.compute_image_bounds(
291
+ pixel_meter_size, frame, beam_width_data,
292
+ additional_pixel_padding_x=0,
293
+ additional_pixel_padding_y=0
294
+ )
295
+
296
+ # Compute the mapping from the samples to the image
297
+ sample_read_rows, sample_read_cols, image_write_rows, image_write_cols = pyARIS.compute_mapping_from_sample_to_image(
298
+ pixel_meter_size,
299
+ xdim, ydim, x_meter_start, y_meter_start,
300
+ frame, beam_width_data
301
+ )
302
 
303
+ marking_mapping = dict(zip(zip(image_write_rows, image_write_cols),
304
+ zip(sample_read_rows, sample_read_cols)))
305
 
306
  # Manual marking format rounds 0.5 to 1 instead of 0 in IEEE 754
307
  def round(number, ndigits=0):
 
314
  metadata["NONDIRECTIONAL_FISH"] = none # TODO
315
  metadata["TOTAL_FISH"] = metadata["UPSTREAM_FISH"] + metadata["DOWNSTREAM_FISH"] + metadata["NONDIRECTIONAL_FISH"]
316
 
317
+ metadata["TOTAL_FRAMES"] = ARISdata.FrameCount if ARISdata is not None else "-"
318
  metadata["EXPECTED_FRAMES"] = -1 # What is this?
319
  metadata["TOTAL_TIME"] = str(datetime.timedelta(seconds=round(metadata["TOTAL_FRAMES"]/metadata["FRAME_RATE"])))
320
  metadata["EXPECTED_TIME"] = str(datetime.timedelta(seconds=round(metadata["EXPECTED_FRAMES"]/metadata["FRAME_RATE"])))
 
323
 
324
  metadata["COUNT_FILE_NAME"] = 'N/A'
325
  metadata["EDITOR_ID"] = 'N/A'
326
+ metadata["INTENSITY"] = f'{round(frame.intensity, 1) if frame is not None else 0:.1f} dB' # Missing
327
+ metadata["THRESHOLD"] = f'{round(frame.threshold, 1) if frame is not None else 0:.1f} dB' # Missing
328
+ metadata["WINDOW_START"] = round(frame.windowstart, 2) if frame is not None else 0
329
+ metadata["WINDOW_END"] = round(frame.windowstart + frame.windowlength, 2) if frame is not None else 0
330
+ metadata["WATER_TEMP"] = f'{int(round(frame.watertemp)) if frame is not None else 0} degC'
331
 
332
  s = f''''''
333
 
 
381
  metadata["FISH"] = []
382
  for entry in sorted(entries, key=lambda x: x['fish_id']):
383
  frame_num = entry['frame_num']
384
+ if ARISdata is not None:
385
+ frame = pyARIS.FrameRead(ARISdata, frame_num)
386
+ else:
387
+ frame = None
388
 
389
  y = (entry['bbox'][1]+entry['bbox'][3])/2
390
  x = (entry['bbox'][0]+entry['bbox'][2])/2
 
406
  fish_entry['NBR_FRAMES'] = entry['end_frame_index'] + 1 - entry['start_frame_index']
407
  fish_entry['TRAVEL'] = entry['travel_dist']
408
  fish_entry['DIR'] = upstream_motion_map[entry['direction']]
409
+ fish_entry['R'] = bin_num * pixel_meter_size + frame.windowstart if frame is not None else "-"
410
  fish_entry['THETA'] = beam_width_data['beam_center'][beam_num]
411
  fish_entry['L'] = entry['length']
412
  fish_entry['DR'] = -1.0 # What is this?
413
  fish_entry['LDR'] = -1.0 # What is this?
414
  fish_entry['ASPECT'] = -1.0 # What is this?
415
+ TIME, DATE = datetime.datetime.fromtimestamp(frame.sonartimestamp/1000000, pytz.timezone('UTC')).strftime('%H:%M:%S %Y-%m-%d').split() if frame is not None else ("-", "-")
416
  fish_entry['TIME'] = TIME
417
  fish_entry['DATE'] = DATE
418
+ fish_entry['LATITUDE'] = frame.latitude or 'N 00 d 0.00000 m' if frame is not None else 0
419
+ fish_entry['LONGITUDE'] = frame.longitude or 'E 000 d 0.00000 m' if frame is not None else 0
420
+ fish_entry['PAN'] = frame.sonarpan if frame is not None else 0
421
  if math.isnan(fish_entry['PAN']): fish_entry['PAN'] = "nan"
422
+ fish_entry['TILT'] = frame.sonartilt if frame is not None else 0
423
  if math.isnan(fish_entry['TILT']): fish_entry['TILT'] = "nan"
424
+ fish_entry['ROLL'] = frame.roll if frame is not None else 0 # May be wrong number but sonarroll was NaN
425
  fish_entry['SPECIES'] = 'Unknown'
426
  fish_entry['MOTION'] = 'Running <-->'
427
  fish_entry['Q'] = -1 #5 # I don't know what this is or where it comes from
 
432
 
433
  # What are these?
434
  # Maybe the date and time range for the recording?
435
+ if ARISdata is not None:
436
+ first_frame = pyARIS.FrameRead(ARISdata, 0)
437
+ last_frame = pyARIS.FrameRead(ARISdata, metadata["TOTAL_FRAMES"]-1)
438
+ start_time, start_date = datetime.datetime.fromtimestamp(first_frame.sonartimestamp/1000000, pytz.timezone('UTC')).strftime('%H:%M:%S %Y-%m-%d').split()
439
+ end_time, end_date = datetime.datetime.fromtimestamp(last_frame.sonartimestamp/1000000, pytz.timezone('UTC')).strftime('%H:%M:%S %Y-%m-%d').split()
440
+ else:
441
+ start_date = 0
442
+ start_time = 0
443
+ end_time = 0
444
  metadata["DATE"] = start_date
445
  metadata["START"] = start_time
446
  metadata["END"] = end_time