@@ -137,7 +137,8 @@ def plot_histogram(
137137 hist_original = (hist_original / hist_original .max ()) * hist .max ()
138138
139139 hist_ = np .argmax (hist )
140- hist_std = np .abs (image - hist_ ).mean ()
140+ image_ = image .astype (np .int32 )
141+ hist_std = np .abs (image_ - hist_ ).mean ()
141142
142143 csum = np .cumsum (hist ) / hist .sum ()
143144 csum_ = np .where (csum >= csum_threshold )[0 ].min ()
@@ -166,7 +167,7 @@ def plot_histogram(
166167 [0 , y_max ],
167168 color = 'grey' ,
168169 linestyle = '--' ,
169- label = f'Histogram Peak ({ hist_ :0.01f} )' ,
170+ label = f'Histogram Peak ({ hist_ :0.01f} +/- { hist_std :0.01f } )' ,
170171 )
171172 plt .plot (
172173 [csum_ ] * 2 ,
@@ -1307,6 +1308,7 @@ def compute_wrapper(
13071308 bands = bands [::- 1 ]
13081309 y_step_freq = float (bands [0 ] - bands [1 ])
13091310 x_step_ms = float ((1e3 * duration ) / stft_db .shape [1 ])
1311+ bands = np .around (bands ).astype (np .int32 ).tolist ()
13101312
13111313 # # Save the spectrogram image to disk
13121314 # cv2.imwrite('debug.tif', stft_db, [cv2.IMWRITE_TIFF_COMPRESSION, 1])
@@ -1346,6 +1348,8 @@ def compute_wrapper(
13461348 }
13471349 metas = []
13481350 for index , (start , stop ) in tqdm .tqdm (list (enumerate (ranges ))):
1351+ if index == 1 :
1352+ break
13491353 segment = stft_db [:, start :stop ]
13501354
13511355 # Step 0.1 - Debugging setup and find peak amplitude (will return None if disabled)
@@ -1424,7 +1428,7 @@ def compute_wrapper(
14241428
14251429 # Step 11 - Collect chirp metadata
14261430 metadata = {
1427- 'curve.(khz ,ms)' : [
1431+ 'curve.(hz ,ms)' : [
14281432 (
14291433 bands [y ],
14301434 (start + x ) * x_step_ms ,
@@ -1438,38 +1442,40 @@ def compute_wrapper(
14381442 'fc.ms' : (start + bands [fc [1 ]]) * x_step_ms ,
14391443 'hi fc:knee.ms' : (start + bands [knee [1 ]]) * x_step_ms ,
14401444 'lo fc:heel.ms' : (start + bands [heel [1 ]]) * x_step_ms ,
1441- 'bandwidth.khz ' : bandwidth ,
1442- 'hi f.khz ' : bands [top ],
1443- 'lo f.khz ' : bands [bottom ],
1444- 'peak f.khz ' : bands [peak [0 ]],
1445- 'fc.khz ' : bands [fc [0 ]],
1446- 'hi fc:knee.khz ' : bands [knee [0 ]],
1447- 'lo fc:heel.khz ' : bands [heel [0 ]],
1445+ 'bandwidth.hz ' : bandwidth ,
1446+ 'hi f.hz ' : bands [top ],
1447+ 'lo f.hz ' : bands [bottom ],
1448+ 'peak f.hz ' : bands [peak [0 ]],
1449+ 'fc.hz ' : bands [fc [0 ]],
1450+ 'hi fc:knee.hz ' : bands [knee [0 ]],
1451+ 'lo fc:heel.hz ' : bands [heel [0 ]],
14481452 'harmonic.flag' : harmonic_flag ,
14491453 'harmonic peak f.ms' : (start + hamonic_peak [1 ]) * x_step_ms if harmonic_flag else None ,
1450- 'harmonic peak f.khz ' : bands [hamonic_peak [0 ]] if harmonic_flag else None ,
1454+ 'harmonic peak f.hz ' : bands [hamonic_peak [0 ]] if harmonic_flag else None ,
14511455 'echo.flag' : echo_flag ,
14521456 'echo peak f.ms' : (start + echo_peak [1 ]) * x_step_ms if echo_flag else None ,
1453- 'echo peak f.khz ' : bands [echo_peak [0 ]] if echo_flag else None ,
1457+ 'echo peak f.hz ' : bands [echo_peak [0 ]] if echo_flag else None ,
14541458 }
14551459 metadata .update (slopes )
14561460
14571461 # Normalize values
14581462 for key , value in list (metadata .items ()):
14591463 if value is None :
14601464 continue
1461- if key .endswith ('.ms' ) or key . endswith ( '.khz' ) :
1465+ if key .endswith ('.ms' ):
14621466 metadata [key ] = round (float (value ), 3 )
1467+ if key .endswith ('.hz' ):
1468+ metadata [key ] = int (round (value ))
14631469 if key .endswith ('.flag' ):
14641470 metadata [key ] = bool (value )
14651471 if key .endswith ('.y_px/x_px' ):
14661472 key_ = key .replace ('.y_px/x_px' , '.khz/ms' )
1467- metadata [key_ ] = round (float (value * (y_step_freq / x_step_ms )), 9 )
1473+ metadata [key_ ] = round (float (value * (( y_step_freq / 1000.0 ) / x_step_ms )), 3 )
14681474 metadata .pop (key )
1469- if key .endswith ('.(khz ,ms)' ):
1475+ if key .endswith ('.(hz ,ms)' ):
14701476 metadata [key ] = [
14711477 (
1472- round (float ( val1 ), 3 ),
1478+ int ( round (val1 )),
14731479 round (float (val2 ), 3 ),
14741480 )
14751481 for val1 , val2 in value
@@ -1488,58 +1494,74 @@ def compute_wrapper(
14881494 if debug_path :
14891495 segments ['canvas' ].append (canvas [:, trim_begin :trim_end ])
14901496
1491- for key in segments :
1497+ for key in list ( segments . keys ()) :
14921498 value = segments [key ]
1493- segments [key ] = np .hstack (value ) if len (value ) > 0 else stft_db .copy ()
1499+ if len (value ) == 0 :
1500+ segments .pop (key )
1501+ continue
1502+ segments [key ] = np .hstack (value )
14941503
14951504 if debug_path :
14961505 cv2 .imwrite (join (debug_path , 'spectrogram.tif' ), stft_db , [cv2 .IMWRITE_TIFF_COMPRESSION , 1 ])
1497- cv2 .imwrite (
1498- join (debug_path , 'spectrogram.compressed.tif' ),
1499- segments ['stft_db' ],
1500- [cv2 .IMWRITE_TIFF_COMPRESSION , 1 ],
1501- )
15021506 cv2 .imwrite (join (debug_path , 'spectrogram.waveplot.png' ), waveplot )
1503- cv2 .imwrite (join (debug_path , 'spectrogram.compressed.waveplot.png' ), segments ['waveplot' ])
1504- temp_top = np .stack ((segments ['stft_db' ], segments ['stft_db' ], segments ['stft_db' ]), axis = 2 )
1505- temp_bot = cv2 .resize (
1506- segments ['waveplot' ], temp_top .shape [:2 ][::- 1 ], interpolation = cv2 .INTER_LINEAR
1507- )
1508- temp_bot = temp_bot .astype (np .float32 ) * (
1509- np .iinfo (temp_top .dtype ).max / np .iinfo (temp_bot .dtype ).max
1510- )
1511- temp_bot = np .around (temp_bot ).astype (temp_top .dtype )
1512- temp = np .vstack ((temp_top , temp_bot ))
1513- cv2 .imwrite (join (debug_path , 'spectrogram.compressed.combined.png' ), temp )
1514- cv2 .imwrite (
1515- join (debug_path , 'spectrogram.compressed.threshold.tif' ),
1516- segments ['costs' ],
1517- [cv2 .IMWRITE_TIFF_COMPRESSION , 1 ],
1518- )
1519- temp = segments ['costs' ].copy ()
1520- flags = segments ['costs' ] == 0
1521- temp = normalize_stft (temp , None , np .uint8 )
1522- temp = cv2 .applyColorMap (temp , cv2 .COLORMAP_JET )
1523- temp [:, :, 0 ][flags ] = 0
1524- temp [:, :, 1 ][flags ] = 0
1525- temp [:, :, 2 ][flags ] = 0
1526- cv2 .imwrite (
1527- join (debug_path , 'spectrogram.compressed.threshold.jet.tif' ),
1528- temp ,
1529- [cv2 .IMWRITE_TIFF_COMPRESSION , 1 ],
1530- )
1531- cv2 .imwrite (
1532- join (debug_path , 'spectrogram.compressed.keypoints.tif' ),
1533- segments ['canvas' ],
1534- [cv2 .IMWRITE_TIFF_COMPRESSION , 1 ],
1535- )
1507+
1508+ if 'stft_db' in segments :
1509+ cv2 .imwrite (
1510+ join (debug_path , 'spectrogram.compressed.tif' ),
1511+ segments ['stft_db' ],
1512+ [cv2 .IMWRITE_TIFF_COMPRESSION , 1 ],
1513+ )
1514+
1515+ if 'waveplot' in segments :
1516+ cv2 .imwrite (join (debug_path , 'spectrogram.compressed.waveplot.png' ), segments ['waveplot' ])
1517+
1518+ if 'stft_db' in segments and 'waveplot' in segments :
1519+ temp_top = np .stack ((segments ['stft_db' ], segments ['stft_db' ], segments ['stft_db' ]), axis = 2 )
1520+ temp_bot = cv2 .resize (
1521+ segments ['waveplot' ], temp_top .shape [:2 ][::- 1 ], interpolation = cv2 .INTER_LINEAR
1522+ )
1523+ temp_bot = temp_bot .astype (np .float32 ) * (
1524+ np .iinfo (temp_top .dtype ).max / np .iinfo (temp_bot .dtype ).max
1525+ )
1526+ temp_bot = np .around (temp_bot ).astype (temp_top .dtype )
1527+ temp = np .vstack ((temp_top , temp_bot ))
1528+ cv2 .imwrite (join (debug_path , 'spectrogram.compressed.combined.png' ), temp )
1529+
1530+ if 'costs' in segments :
1531+ cv2 .imwrite (
1532+ join (debug_path , 'spectrogram.compressed.threshold.tif' ),
1533+ segments ['costs' ],
1534+ [cv2 .IMWRITE_TIFF_COMPRESSION , 1 ],
1535+ )
1536+ temp = segments ['costs' ].copy ()
1537+ flags = segments ['costs' ] == 0
1538+ temp = normalize_stft (temp , None , np .uint8 )
1539+ temp = cv2 .applyColorMap (temp , cv2 .COLORMAP_JET )
1540+ temp [:, :, 0 ][flags ] = 0
1541+ temp [:, :, 1 ][flags ] = 0
1542+ temp [:, :, 2 ][flags ] = 0
1543+ cv2 .imwrite (
1544+ join (debug_path , 'spectrogram.compressed.threshold.jet.tif' ),
1545+ temp ,
1546+ [cv2 .IMWRITE_TIFF_COMPRESSION , 1 ],
1547+ )
1548+
1549+ if 'canvas' in segments :
1550+ cv2 .imwrite (
1551+ join (debug_path , 'spectrogram.compressed.keypoints.tif' ),
1552+ segments ['canvas' ],
1553+ [cv2 .IMWRITE_TIFF_COMPRESSION , 1 ],
1554+ )
15361555
15371556 output_paths = []
15381557 compressed_paths = []
15391558 datas = [
15401559 (output_paths , 'jpg' , stft_db ),
1541- (compressed_paths , 'compressed.jpg' , segments ['stft_db' ]),
15421560 ]
1561+ if 'stft_db' in segments :
1562+ datas += [
1563+ (compressed_paths , 'compressed.jpg' , segments ['stft_db' ]),
1564+ ]
15431565
15441566 for accumulator , tag , data in datas :
15451567 if data .dtype != np .uint8 :
@@ -1568,29 +1590,31 @@ def compute_wrapper(
15681590 metadata = {
15691591 'wav.path' : wav_filepath ,
15701592 'spectrogram' : {
1571- 'true .path' : output_paths ,
1593+ 'uncompressed .path' : output_paths ,
15721594 'compressed.path' : compressed_paths ,
15731595 },
15741596 'threshold.amp' : int (round (255.0 * (threshold / max_value ))),
1575- 'sr.khz ' : sr ,
1597+ 'sr.hz ' : int ( sr ) ,
15761598 'duration.ms' : round (duration * 1e3 , 3 ),
15771599 'frequencies' : {
1578- 'min.khz ' : int (FREQ_MIN ),
1579- 'max.khz ' : int (FREQ_MAX ),
1580- 'pixels.khz ' : [ round ( float ( band ), 3 ) for band in bands ] ,
1600+ 'min.hz ' : int (FREQ_MIN ),
1601+ 'max.hz ' : int (FREQ_MAX ),
1602+ 'pixels.hz ' : bands ,
15811603 },
15821604 'size' : {
1583- 'true ' : {
1605+ 'uncompressed ' : {
15841606 'width.px' : stft_db .shape [1 ],
15851607 'height.px' : stft_db .shape [0 ],
15861608 },
1587- 'compressed' : {
1588- 'width.px' : segments ['stft_db' ].shape [1 ],
1589- 'height.px' : segments ['stft_db' ].shape [0 ],
1590- },
1609+ 'compressed' : None ,
15911610 },
15921611 'segments' : metas ,
15931612 }
1613+ if 'stft_db' in segments :
1614+ metadata ['size' ]['compressed' ] = {
1615+ 'width.px' : segments ['stft_db' ].shape [1 ],
1616+ 'height.px' : segments ['stft_db' ].shape [0 ],
1617+ },
15941618
15951619 metadata_path = join (output_folder , f'{ base } .metadata.json' )
15961620 with open (metadata_path , 'w' ) as metafile :
0 commit comments