Modify your send.py program to send all the files in the “samples” directory for classification and print out the results.
时间: 2024-04-12 16:30:51 浏览: 17
Sure! Here's an example of how you can modify the `send.py` program to send all the files in the "samples" directory for classification and print out the results:
```python
import os
import requests
# API endpoint for classification
classification_url = "http://localhost:8000/classify"
# Directory containing sample files
samples_dir = "samples"
def classify_file(file_path):
# Open the file in binary mode and read the data
with open(file_path, "rb") as file:
file_data = file.read()
# Send a POST request to the classification API
response = requests.post(classification_url, files={"file": file_data})
# Print the classification result
print(f"File: {file_path}")
print("Classification Result:", response.json())
print()
def main():
# Iterate over all files in the samples directory
for filename in os.listdir(samples_dir):
file_path = os.path.join(samples_dir, filename)
classify_file(file_path)
if __name__ == "__main__":
main()
```
In this modified version of `send.py`, we first define the API endpoint for classification (`classification_url`) and the directory containing the sample files (`samples_dir`).
The `classify_file` function takes a file path as input. It opens the file in binary mode, reads its data, and then sends a POST request to the classification API with the file data. The response received from the API is then printed as the classification result.
In the `main` function, we iterate over all files in the `samples` directory using `os.listdir`. For each file, we construct its full path using `os.path.join` and then call the `classify_file` function to classify that file.
Make sure to replace the `classification_url` with the actual URL of your classification API.
Hope this helps! Let me know if you have any further questions.