我有一个python脚本,可以以10-15 FPS生成一些图像。我需要使用flask应用程序广播这些图像(流)。
app.py
function nextpaymentdatechange( $order_id ){
// YOUR SETTINGS: Set in this array your desired dates (value(s)) by product Id (key)
$dates_for_product = array(
876 => array(
'next_payment' => '2020-11-15 07:00:00',
'last_payment' => '2020-11-16 07:00:00',
),
877 => array(
'next_payment' => '2021-02-15 07:00:00',
'last_payment' => '2021-02-16 07:00:00',
),
878 => array(
'next_payment' => '2021-08-03 07:00:00',
'last_payment' => '2021-08-04 07:00:00',
),
);
// The date types for subscriptions
$suscription_date_types = array('start', 'trial_end', 'next_payment', 'last_payment', 'end');
// Get the subscriptions from the current order Id
$subscriptions = wcs_get_subscriptions_for_order( $order_id );
// Loop through subscriptions
foreach ( $subscriptions as $subscription_id => $subscription ) {
// Loop through items in this subscription
foreach ( $subscription->get_items() as $item_id => $item ) {
$product = $item->get_product();
// Loop through defined products dates
foreach( $dates_for_product as $product_id => $new_dates ) {
// If current subscription product id matches
if ( $product->get_id() == $product_id ) {
$current_product_id = $product_id; // Set current product id
break; // Stop the current loop
}
}
if ( isset($current_product_id) ) {
break; // Stop the current loop
}
}
// Updating subscription dates
if ( isset($current_product_id) ) {
$updated_dates = array(); // Initializing
// Loop through subscription date types
foreach( $suscription_date_types as $date_type ) {
$date = $subscription->get_date($date_type);
// For 'next_payment' and 'last_payment' dates
if( isset($new_dates[$date_type]) && $subscription->can_date_be_updated($date_type) ) {
$updated_dates[$date_type] = $new_dates[$date_type];
}
// For 'end' date
elseif ( $date_type === 'end' && $subscription->can_date_be_updated($date_type) ) {
$updated_dates[$date_type] = $new_dates['last_payment']; // ??? Or may be instead: $date; … (or simply: 0;)
}
// Other dates
else {
$updated_dates[$date_type] = $date;
}
}
// Update subscription date, save to database and refresh cached data
$subscription->update_dates($updated_dates);
$subscription->save();
}
}
}
templates / index.html
from flask import Flask, render_template, Response
import cv2
import numpy as np
import gc
app = Flask(__name__)
class Stream:
def __init__(self):
pass
def get_frame(self):
frame = cv2.imread('now.jpg')
return frame, 1
stream = Stream()
@app.route('/')
def index():
return render_template('index.html')
def gen(stream):
while True:
frame, frames_left = stream.get_frame() # get processed frame
try:
if frame.shape != (1080, 1920, 3):
# not proper dimension
frame = cv2.resize(frame, (1920, 1080))
except:
print('failed')
frame = np.random.randint(0, 1, (1080,1920,3))
(flag, encodedImage) = cv2.imencode(".jpg", frame)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(stream),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=6001, debug=False)
因此,现在程序1正在编写名称为<!DOCTYPE html>
<html>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body,h1,h2,h3,h4,h5,h6 {font-family: "Raleway", Arial, Helvetica, sans-serif}
.mySlides {display: none}
</style>
<body class="w3-content w3-border-left w3-border-right">
<!-- !PAGE CONTENT! -->
<div class="w3-main w3-white" style="margin-left:0px">
<!-- Push down content on small screens -->
<div class="w3-hide-large" style="margin-top:80px"></div>
<!-- Slideshow Header -->
<div class="w3-container" id="apartment">
<h2 class="w3-text-green"></h2>
<div class="w3-display-container mySlides">
<img src="{{ url_for('video_feed') }}" style="width:100%;margin-bottom:-6px">
<div class="w3-display-bottomleft w3-container w3-black">
<p>Straming ...</p>
</div>
</div>
</div>
<script>
// Script to open and close sidebar when on tablets and phones
function w3_open() {
document.getElementById("mySidebar").style.display = "block";
document.getElementById("myOverlay").style.display = "block";
}
function w3_close() {
document.getElementById("mySidebar").style.display = "none";
document.getElementById("myOverlay").style.display = "none";
}
// Slideshow Apartment Images
var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
showDivs(slideIndex += n);
}
function currentDiv(n) {
showDivs(slideIndex = n);
}
function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
if (n > x.length) {slideIndex = 1}
if (n < 1) {slideIndex = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" w3-opacity-off", "");
}
x[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " w3-opacity-off";
}
</script>
</body>
</html>
的框架,而flask应用程序正在读取并显示最新的框架。一切正常,但问题是在某些情况下,flask应用正在读取不完整的图像,因为这些图像仍在写入中。警告:now.jpg
如何在程序1和flask应用程序之间共享图像,以便一旦由程序1完全写入图像就可以读取它们?