preprocess_gray_image.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import cv_bridge
4 import numpy as np
5 import os.path as osp
6 import rospkg
7 import rospy
8 from sensor_msgs.msg import Image
9 from topic_tools import LazyTransport
10 from sound_classification.process_gray_image import spectral_subtract, normalize_gray_image
11 
12 
14  """
15  This class is to preprocess gray spectrogram for classifying.
16  1. Spectral subtraction by spectral subtraction method
17  # 2. Smooth spectrogram
18  # 3. Normalize spectrogram (32FC1 -> 8UC1, make each pixel value 0 ~ 255)
19  """
20 
21  def __init__(self):
22  super(self.__class__, self).__init__()
23  # Noise subtraction
24  rospack = rospkg.RosPack()
25  self.train_dir = osp.join(rospack.get_path(
26  'sound_classification'), 'train_data')
27  self.noise_data_path = osp.join(self.train_dir, 'noise.npy')
28  if osp.exists(self.noise_data_path):
29  noise_data = np.load(self.noise_data_path)
30  self.mean_spectrum = np.mean(noise_data, axis=0)
31  else:
32  rospy.logwarn('{} is not found.'.format(self.noise_data_path))
33  self.mean_spectrum = 0
34  # Publisher and Subscriber
35  self.bridge = cv_bridge.CvBridge()
36  self.pub = self.advertise('~output', Image, queue_size=1)
38  '~output_normalized', Image, queue_size=1)
39  self.subscribe()
40 
41  def subscribe(self):
42  self.sub = rospy.Subscriber('~input', Image, self._process,
43  queue_size=1, buff_size=2**24)
44 
45  def unsubscribe(self):
46  self.sub.unregister()
47 
48  def _process(self, imgmsg):
49  raw_img = self.bridge.imgmsg_to_cv2(imgmsg)
50  # Spectral subtract
51  subtracted_img = spectral_subtract(raw_img, self.mean_spectrum)
52  # Normalize
53  normalized_img = normalize_gray_image(subtracted_img)
54  # 32FC1 -> 8UC1
55  normalized_img = normalized_img.astype(np.uint8)
56  # Publish
57  # Spectral subtracted image
58  pubmsg = self.bridge.cv2_to_imgmsg(subtracted_img)
59  pubmsg.header = imgmsg.header
60  self.pub.publish(pubmsg)
61  # Normalized img
62  pubmsg = self.bridge.cv2_to_imgmsg(normalized_img)
63  pubmsg.header = imgmsg.header
64  self.pub_normalized.publish(pubmsg)
65 
66 
67 if __name__ == '__main__':
68  rospy.init_node('preprocess_gray_image')
70  rospy.spin()
preprocess_gray_image.PreprocessGrayImage.unsubscribe
def unsubscribe(self)
Definition: preprocess_gray_image.py:45
preprocess_gray_image.PreprocessGrayImage._process
def _process(self, imgmsg)
Definition: preprocess_gray_image.py:48
preprocess_gray_image.PreprocessGrayImage.bridge
bridge
Definition: preprocess_gray_image.py:35
topic_tools::LazyTransport::subscribe
def subscribe(self)
preprocess_gray_image.PreprocessGrayImage.pub_normalized
pub_normalized
Definition: preprocess_gray_image.py:37
topic_tools::LazyTransport::advertise
def advertise(self, *args, **kwargs)
sound_classification.process_gray_image
Definition: process_gray_image.py:1
preprocess_gray_image.PreprocessGrayImage.noise_data_path
noise_data_path
Definition: preprocess_gray_image.py:27
sound_classification.process_gray_image.normalize_gray_image
def normalize_gray_image(img)
Definition: process_gray_image.py:25
preprocess_gray_image.PreprocessGrayImage.pub
pub
Definition: preprocess_gray_image.py:36
topic_tools::LazyTransport
preprocess_gray_image.PreprocessGrayImage
Definition: preprocess_gray_image.py:13
preprocess_gray_image.PreprocessGrayImage.__init__
def __init__(self)
Definition: preprocess_gray_image.py:21
preprocess_gray_image.PreprocessGrayImage.subscribe
def subscribe(self)
Definition: preprocess_gray_image.py:41
preprocess_gray_image.PreprocessGrayImage.train_dir
train_dir
Definition: preprocess_gray_image.py:25
preprocess_gray_image.PreprocessGrayImage.sub
sub
Definition: preprocess_gray_image.py:42
preprocess_gray_image.PreprocessGrayImage.mean_spectrum
mean_spectrum
Definition: preprocess_gray_image.py:30
sound_classification.process_gray_image.spectral_subtract
def spectral_subtract(img, noise)
Definition: process_gray_image.py:6


sound_classification
Author(s):
autogenerated on Fri May 16 2025 03:12:55