prediction.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # predict_location.py
  2. import subprocess
  3. import joblib
  4. import pandas as pd
  5. # Function to scan Wi-Fi networks (same as in wifi_scan.py)
  6. def get_wifi_signal():
  7. scan_output = subprocess.check_output(['sudo', 'iwlist', 'wlan0', 'scan']).decode('utf-8')
  8. return scan_output
  9. # Function to parse signal strength from scan output
  10. def parse_signal_strength(scan_output):
  11. signal_strengths = []
  12. for line in scan_output.split('\n'):
  13. if "Signal level=" in line:
  14. strength = line.split("Signal level=")[1].split(" ")[0]
  15. signal_strengths.append(int(strength))
  16. return signal_strengths
  17. # Load the pre-trained model
  18. model = joblib.load('wifi_model.pkl')
  19. # Predict the location based on real-time signal strengths
  20. def predict_location():
  21. scan_output = get_wifi_signal()
  22. signal_strengths = parse_signal_strength(scan_output)
  23. # Prepare the input as a DataFrame with the correct feature name
  24. for strength in signal_strengths:
  25. data = pd.DataFrame([[strength]], columns=['SignalStrength']) # Use the feature name 'SignalStrength'
  26. predicted_location = model.predict(data)
  27. print(f'Predicted Location: {predicted_location[0]}')
  28. # Run the prediction
  29. predict_location()