-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,19 @@ | ||
# drought-monitor | ||
ChatGPT written code to create drought monitoring video | ||
## ChatGPT written code to create drought monitoring video | ||
|
||
Used publicly available ChatGPT https://chat.openai.com/chat and the following prompts to generate the code | ||
|
||
## prompt1 | ||
|
||
```Write a python code to output a list of URLs in a file where each URL is in the form https://droughtmonitor.unl.edu/data/png/<date>/<date>_usdm.png where <date> is in the form YYYYMMDD ranges from 20000104 to 20230314 incremented by 7 days``` | ||
|
||
|
||
The prompt is shown in the [screenshot prompt1](prompt1.png). The output is captured in the file [generate_urls.py](generate_urls.py) | ||
|
||
There are state or region specific drought map images available in the site https://droughtmonitor.unl.edu/maps/maparchive.aspx. For example, to get the drought maps of california use the URL pattern `https://droughtmonitor.unl.edu/data/png/<date>/<date>_ca_trd.png` | ||
|
||
## prompt2 | ||
```Write a python code which takes a file named urls.txt containing list of image urls as input and generates a mp4 video with those images. Give 0.1 sec delay between downloading subsequent images ``` | ||
|
||
The prompt is shown in the [screenshot prompt2](prompt2.png). The output is captured in the file [create_video.py](create_video.py) | ||
|
||
It missed importing numpy which I had to manually add. I also added the print statement to monitor the progress. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import cv2 | ||
import numpy as np | ||
import urllib.request | ||
|
||
# Define the URLs file name | ||
file_name = 'urls.txt' | ||
|
||
# Define the video frame size and FPS | ||
width, height = 640, 360 | ||
fps = 30 | ||
|
||
# Create a VideoWriter object to save the video | ||
fourcc = cv2.VideoWriter_fourcc(*'mp4v') | ||
out = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height)) | ||
|
||
# Open the URLs file and iterate over the lines | ||
with open(file_name) as f: | ||
for line in f: | ||
# Remove any leading/trailing whitespaces and line breaks | ||
url = line.strip() | ||
try: | ||
print("Processing", url) | ||
# Download the image from the URL | ||
with urllib.request.urlopen(url) as url_response: | ||
img_array = bytearray(url_response.read()) | ||
|
||
# Convert the byte array to a numpy array and reshape it to the desired frame size | ||
img = np.asarray(img_array, dtype=np.uint8) | ||
img = cv2.imdecode(img, cv2.IMREAD_UNCHANGED) | ||
img = cv2.resize(img, (width, height)) | ||
|
||
# Add the frame to the video | ||
out.write(img) | ||
|
||
# Wait for 0.1 seconds before downloading the next image | ||
cv2.waitKey(100) | ||
|
||
except: | ||
# If there is an error in downloading the image, skip it and continue with the next URL | ||
continue | ||
|
||
# Release the VideoWriter object and destroy any remaining windows | ||
out.release() | ||
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import datetime | ||
|
||
# Define start and end dates | ||
start_date = datetime.date(2000, 1, 4) | ||
end_date = datetime.date(2023, 3, 14) | ||
|
||
# Define the URL format string | ||
url_template = 'https://droughtmonitor.unl.edu/data/png/{date}/{date}_usdm.png' | ||
|
||
# Define the output file name | ||
output_file = 'urls.txt' | ||
|
||
# Open the file for writing | ||
with open(output_file, 'w') as f: | ||
# Loop over dates in 7-day increments | ||
delta = datetime.timedelta(days=7) | ||
d = start_date | ||
while d <= end_date: | ||
# Format the URL for the current date | ||
url = url_template.format(date=d.strftime('%Y%m%d')) | ||
# Write the URL to the file | ||
f.write(url + '\n') | ||
# Increment the date | ||
d += delta |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.