Joe Black
Joe Black
CC#
Created by Joe Black on 11/25/2024 in #help
Hi guys i have a problem with my c# - python code!
Its not necessary because the same code works in just python solution but dont work when i start it in c#. I already had an advice about ctrl+f for .where so it's not the reason for exception
12 replies
CC#
Created by Joe Black on 11/25/2024 in #help
Hi guys i have a problem with my c# - python code!
No description
12 replies
CC#
Created by Joe Black on 11/25/2024 in #help
Hi guys i have a problem with my c# - python code!
No description
12 replies
CC#
Created by Joe Black on 11/25/2024 in #help
Hi guys i have a problem with my c# - python code!
@canton7
12 replies
CC#
Created by Joe Black on 11/25/2024 in #help
Hi guys i have a problem with my c# - python code!
No description
12 replies
CC#
Created by Joe Black on 11/25/2024 in #help
Hi guys i have a problem with my c# - python code!
No description
12 replies
CC#
Created by Joe Black on 11/25/2024 in #help
Hi guys i have a problem with my c# - python code!
There's python code
//python code
def ImageAnalyze(filePath):
import os
from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np
model = load_model('D:\\VisualStudio\\RoadSignImageAnalyzer\\bin\\Debug\\net8.0-windows\\traffic_classifier.keras', compile = False)
if os.path.exists('D:\\VisualStudio\\RoadSignImageAnalyzer\\bin\\Debug\\net8.0-windows\\traffic_classifier.keras') is False:
return "Keras not found"
def preprocess_image(image_path):
try:
image = Image.open(image_path)
image = image.resize((30, 30))
image = np.array(image)

image = np.expand_dims(image, axis=0)
return image
except Exception as e:
print(f"Error processing image: {e}")
return None
processed_image = preprocess_image('D:\\VisualStudio\\RoadSignImageAnalyzer\\bin\\Debug\\net8.0-windows\\12630.png')
if os.path.exists('D:\\VisualStudio\\RoadSignImageAnalyzer\\bin\\Debug\\net8.0-windows\\12630.png') is False:
return "Phott not found"
if processed_image is not None:
prediction = model.predict(processed_image)
predicted_class = np.argmax(prediction, axis=1)[0]
message = f"Predicted Class: {predicted_class}"
return message
else:
return "Error: Unable to process image"
//python code
def ImageAnalyze(filePath):
import os
from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np
model = load_model('D:\\VisualStudio\\RoadSignImageAnalyzer\\bin\\Debug\\net8.0-windows\\traffic_classifier.keras', compile = False)
if os.path.exists('D:\\VisualStudio\\RoadSignImageAnalyzer\\bin\\Debug\\net8.0-windows\\traffic_classifier.keras') is False:
return "Keras not found"
def preprocess_image(image_path):
try:
image = Image.open(image_path)
image = image.resize((30, 30))
image = np.array(image)

image = np.expand_dims(image, axis=0)
return image
except Exception as e:
print(f"Error processing image: {e}")
return None
processed_image = preprocess_image('D:\\VisualStudio\\RoadSignImageAnalyzer\\bin\\Debug\\net8.0-windows\\12630.png')
if os.path.exists('D:\\VisualStudio\\RoadSignImageAnalyzer\\bin\\Debug\\net8.0-windows\\12630.png') is False:
return "Phott not found"
if processed_image is not None:
prediction = model.predict(processed_image)
predicted_class = np.argmax(prediction, axis=1)[0]
message = f"Predicted Class: {predicted_class}"
return message
else:
return "Error: Unable to process image"
it works well. libraries also imports well(i checked the possibility to return every library version as string from that method so libraries is not a problem. Also filepath is fine and kernel trained model is fine too. This code works in another solution or in vs code jupiter notebook or even from my desctop in cmd/ But when i start it from c# it stops on that line
prediction = model.predict(processed_image)
prediction = model.predict(processed_image)
and then i heave this kind of exception Python.Runtime.PythonException: ''NoneType' object has no attribute 'write''
12 replies
CC#
Created by Joe Black on 11/25/2024 in #help
Hi guys i have a problem with my c# - python code!
private void analyzeBtn_Click(object sender, EventArgs e) {
if (String.IsNullOrEmpty(this.filePathLb.Text) == true) return;

//python(version) dll path on machine
Runtime.PythonDLL = @"C:\Users\D1mentrrr\AppData\Local\Programs\Python\Python311\python311.dll";
PythonEngine.Initialize();

using (Py.GIL()) {
// Import sys module
dynamic sys = Py.Import("sys");

//current bin/debug directory
sys.path.append(@".");

// Python scrypt
dynamic script = Py.Import("scrypt");
string message = script.ImageAnalyze("D:\\VisualStudio\\PythonApplication1\\12630.png"); // dont look at the path its not used and its just a plug for function

MessageBox.Show(message);
}
}
private void analyzeBtn_Click(object sender, EventArgs e) {
if (String.IsNullOrEmpty(this.filePathLb.Text) == true) return;

//python(version) dll path on machine
Runtime.PythonDLL = @"C:\Users\D1mentrrr\AppData\Local\Programs\Python\Python311\python311.dll";
PythonEngine.Initialize();

using (Py.GIL()) {
// Import sys module
dynamic sys = Py.Import("sys");

//current bin/debug directory
sys.path.append(@".");

// Python scrypt
dynamic script = Py.Import("scrypt");
string message = script.ImageAnalyze("D:\\VisualStudio\\PythonApplication1\\12630.png"); // dont look at the path its not used and its just a plug for function

MessageBox.Show(message);
}
}
Its c# code that needs to get just a string from my python code. I'm using pythonnet here for that.
12 replies