0% found this document useful (0 votes)
7 views3 pages

Morse Code Receiver with OpenCV

The document contains a Python script for a Morse code receiver using OpenCV to capture video input and decode Morse signals from LED light. It defines thresholds for distinguishing dots and dashes, manages the decoding process, and displays the decoded message along with other visual feedback. The program includes functionality for adjusting brightness thresholds and controlling the decoding state through keyboard inputs.

Uploaded by

tejaravi1611
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Morse Code Receiver with OpenCV

The document contains a Python script for a Morse code receiver using OpenCV to capture video input and decode Morse signals from LED light. It defines thresholds for distinguishing dots and dashes, manages the decoding process, and displays the decoded message along with other visual feedback. The program includes functionality for adjusting brightness thresholds and controlling the decoding state through keyboard inputs.

Uploaded by

tejaravi1611
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

RECEIVER CODE:-

import cv2
import numpy as np
import time
from collections import deque

# Adjusted timing thresholds (seconds)


DOT_DASH_THRESH = 0.35
LETTER_GAP_THRESH = 0.55
WORD_GAP_THRESH = 1.2

INITIAL_BRIGHTNESS_THRESH = 180
ROI_SIZE = 100

BRIGHTNESS_SMOOTHING = 5

MORSE_TO_CHAR = {

'.-':'A','-...':'B','-.-.':'C','-..':'D','.':'E','..-.':'F','--.':'G','....':'H',

'..':'I','.---':'J','-.-':'K','.-..':'L','--':'M','-.':'N','---':'O','.--.':'P',

'--.-':'Q','.-.':'R','...':'S','-':'T','..-':'U','...-':'V','.--':'W','-..-':'X',

'-.--':'Y','--..':'Z','-----':'0','.----':'1','..---':'2','...--':'3','....-':'4',
'.....':'5','-....':'6','--...':'7','---..':'8','----.':'9'
}

def make_trackbar_window(initial_thresh):
[Link]('Morse Receiver', cv2.WINDOW_NORMAL)
[Link]('BrightThresh', 'Morse Receiver', initial_thresh, 255,
lambda x: None)

def get_trackbar_thresh():
return [Link]('BrightThresh', 'Morse Receiver')

def draw_text(img, text, pos, scale=0.6, color=(255,255,255), thickness=1,


bgcolor=(0,0,0)):
x,y = pos
[Link](img, (x-2,y-18), (x + int(9*len(text)*scale), y+4), bgcolor, -1)
[Link](img, text, (x,y), cv2.FONT_HERSHEY_SIMPLEX, scale, color,
thickness, cv2.LINE_AA)

class MorseDecoder:
def __init__(self):
[Link]()

def reset(self):
self.current_symbol = ''
self.current_letter = ''
self.current_word = ''
self.decoded_words = []
self.led_state = False
self.state_change_time = [Link]()
self.last_off_time = [Link]()
[Link] = True

def update_state(self, is_on, now):


duration = now - self.state_change_time

# Transition detected
if is_on != self.led_state:
if not is_on: # LED just turned off
if duration < DOT_DASH_THRESH:
self.current_symbol += '.'
else:
self.current_symbol += '-'
self.last_off_time = now
else: # LED just turned on
gap = now - self.last_off_time
if gap >= WORD_GAP_THRESH:
self._flush_symbol_to_letter()
self._flush_letter_to_word()
elif gap >= LETTER_GAP_THRESH:
self._flush_symbol_to_letter()

self.led_state = is_on
self.state_change_time = now

# If LED stays off for a long time (end of message)


if not is_on and (now - self.last_off_time) > WORD_GAP_THRESH:
self._flush_symbol_to_letter()
self._flush_letter_to_word()
self.last_off_time = now

def _flush_symbol_to_letter(self):
if self.current_symbol:
letter = MORSE_TO_CHAR.get(self.current_symbol, '?')
self.current_letter = letter
self.current_word += letter
self.current_symbol = ''

def _flush_letter_to_word(self):
if self.current_word:
self.decoded_words.append(self.current_word)
self.current_word = ''

def get_decoded_text(self):
text = ' '.join(self.decoded_words)
if self.current_word:
text += ' ' + self.current_word
return [Link]()

def get_status(self):
return {
'symbol': self.current_symbol,
'letter': self.current_letter,
'word': self.current_word,
'decoded': self.get_decoded_text(),
'led_state': self.led_state
}

def main():
cap = [Link](0)
if not [Link]():
print("Webcam not found.")
return
make_trackbar_window(INITIAL_BRIGHTNESS_THRESH)
decoder = MorseDecoder()
brightness_history = deque(maxlen=BRIGHTNESS_SMOOTHING)

try:
while True:
ret, frame = [Link]()
if not ret:
break

h, w = [Link][:2]
cx, cy = w//2, h//2
r = ROI_SIZE//2
roi = frame[cy-r:cy+r, cx-r:cx+r]
gray = [Link](roi, cv2.COLOR_BGR2GRAY)
mean_bright = float([Link](gray))
brightness_history.append(mean_bright)
smooth_bright = [Link](brightness_history)
thresh = get_trackbar_thresh()
is_on = smooth_bright > thresh
now = [Link]()

if [Link]:
decoder.update_state(is_on, now)

s = decoder.get_status()
vis = [Link]()
[Link](vis, (cx-r, cy-r), (cx+r, cy+r), (0,255,0), 2)
draw_text(vis, f'Brightness: {smooth_bright:.1f}', (10,30))
draw_text(vis, f'Thresh: {thresh}', (10,55))
draw_text(vis, f'LED: {"ON" if is_on else "OFF"}', (10,80))
draw_text(vis, f'Symbol: {s["symbol"]}', (10,110))
draw_text(vis, f'Letter: {s["letter"]}', (10,135))
draw_text(vis, f'Word: {s["word"]}', (10,160))
draw_text(vis, f'Decoded: {s["decoded"]}', (10,195), scale=0.7)
draw_text(vis, "s=start/stop r=reset q=quit", (10,230), scale=0.5)
small = [Link](roi, (120,120))
vis[10:130, w-130:w-10] = small
[Link]('Morse Receiver', vis)

key = [Link](1) & 0xFF


if key in [ord('q'), 27]:
break
elif key == ord('s'):
[Link] = not [Link]
elif key == ord('r'):
[Link]()
finally:
[Link]()
[Link]()
print("\nFinal decoded message:", decoder.get_decoded_text())

if __name__ == "__main__":
main()

You might also like