1234567891011121314151617181920212223242526272829303132333435 |
- # predict_location.py
- import subprocess
- import joblib
- import pandas as pd
- # Function to scan Wi-Fi networks (same as in wifi_scan.py)
- def get_wifi_signal():
- scan_output = subprocess.check_output(['sudo', 'iwlist', 'wlan0', 'scan']).decode('utf-8')
- return scan_output
- # Function to parse signal strength from scan output
- def parse_signal_strength(scan_output):
- signal_strengths = []
- for line in scan_output.split('\n'):
- if "Signal level=" in line:
- strength = line.split("Signal level=")[1].split(" ")[0]
- signal_strengths.append(int(strength))
- return signal_strengths
- # Load the pre-trained model
- model = joblib.load('wifi_model.pkl')
- # Predict the location based on real-time signal strengths
- def predict_location():
- scan_output = get_wifi_signal()
- signal_strengths = parse_signal_strength(scan_output)
- # Prepare the input as a DataFrame with the correct feature name
- for strength in signal_strengths:
- data = pd.DataFrame([[strength]], columns=['SignalStrength']) # Use the feature name 'SignalStrength'
- predicted_location = model.predict(data)
- print(f'Predicted Location: {predicted_location[0]}')
- # Run the prediction
- predict_location()
|