File size: 3,478 Bytes
1e5c067 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | # app.py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
from PIL import Image
import json
import csv
from io import BytesIO
import requests
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from geopy.distance import geodesic
import osmnx as ox
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from flask import Flask, request, render_template
app = Flask(__name__)
# Read data
pd.set_option('display.max_columns', None)
df = pd.read_json("./Tourist_related.json")
# Convert data to CSV
with open('./data.csv', 'w') as data_file:
csv_writer = csv.writer(data_file)
csv_writer.writerow(df.columns)
csv_writer.writerows(df.values)
# Read CSV data
read_dat = pd.read_csv('./data.csv', on_bad_lines='skip')
# Function for data quality test
def data_quality_test():
check_na = read_dat.isnull().sum()
for features, count in check_na.items():
print(f'{features}: {count}')
# Function for correlation analysis
def correlation_analysis():
plt.figure(figsize=(12, 6))
sns.heatmap(df.corr(), annot=True)
plt.show()
# Geo Plot Visualization Function
def geo_plot_visualization():
df['lat'] = df['location'].apply(lambda x: x.get('lat', None))
df['lng'] = df['location'].apply(lambda x: x.get('lng', None))
color_scale = [(0, 'orange'), (1, 'red')]
fig = px.scatter_mapbox(df,
lat="lat",
lon="lng",
color_continuous_scale=color_scale,
zoom=8,
height=800,
width=800)
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.show()
# Function for image category recognition
def predict_image_category(image_url):
# Replace this part with your image classification logic
# For example, you can use a pre-trained deep learning model
# Here, I'm using a placeholder
categories = ['Category A', 'Category B', 'Category C']
prediction = np.random.choice(categories)
return prediction
# Random Forest Classifier
def train_random_forest():
# Assuming 'target' is the target variable
X = df.drop('target', axis=1)
y = df['target']
# Assuming 'categorical_columns' is a list of categorical columns
le = LabelEncoder()
for col in categorical_columns:
X[col] = le.fit_transform(X[col])
# Create and train the random forest classifier
rf_classifier = RandomForestClassifier()
rf_classifier.fit(X, y)
return rf_classifier
# Home route
@app.route('/')
def home():
return render_template('index.html')
# Image upload route
@app.route('/upload', methods=['POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
if file:
# Read the uploaded image
img = Image.open(file.stream)
# Display the image
plt.imshow(img)
plt.axis('off')
plt.show()
# Predict the image category
prediction = predict_image_category(file.stream)
return render_template('result.html', prediction=prediction)
# Start the Flask app
if __name__ == '__main__':
app.run(debug=True) |